静态同步synchronized方法与synchronized(class)代码块

关键字synchronized应用在static静态方法上,这样锁的是当前Class类进行加锁。同步执行操作

public class ThreadA extends Thread {
	@Override
	public void run() {
		Service.printA();
	}

}

public class ThreadB extends Thread {
	@Override
	public void run() {
		Service.printB();
	}
}

public class Service {

	synchronized public static void printA() {
		try {
			System.out.println("线程名称为:" + Thread.currentThread().getName()
					+ "在" + System.currentTimeMillis() + "进入printA");
			Thread.sleep(3000);
			System.out.println("线程名称为:" + Thread.currentThread().getName()
					+ "在" + System.currentTimeMillis() + "离开printA");
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
	}

	synchronized public static void printB() {
		System.out.println("线程名称为:" + Thread.currentThread().getName() + "在"
				+ System.currentTimeMillis() + "进入printB");
		System.out.println("线程名称为:" + Thread.currentThread().getName() + "在"
				+ System.currentTimeMillis() + "离开printB");
	}

}
public static void main(String[] args) {

		ThreadA a = new ThreadA();
		a.setName("A");
		a.start();

		ThreadB b = new ThreadB();
		b.setName("B");
		b.start();

	}

synchronized关键字加static静态方法和synchronized加到非static方法上的区别:一个锁的是class类,一个锁的是当前的对象。(这里不再赘述代码,思路:一个资源类中3个方法 2个static的 1一个非static,创建三个线程分别调用1 2 3个方法来测试)

同步synchronized(class)代码块的作用和synchronized static方法作用一样。


public class Service {

	synchronized public static void printA() {
		try {
			System.out.println("线程名称为:" + Thread.currentThread().getName()
					+ "在" + System.currentTimeMillis() + "进入printA");
			Thread.sleep(3000);
			System.out.println("线程名称为:" + Thread.currentThread().getName()
					+ "在" + System.currentTimeMillis() + "离开printA");
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
	}

	synchronized public static void printB() {
		System.out.println("线程名称为:" + Thread.currentThread().getName() + "在"
				+ System.currentTimeMillis() + "进入printB");
		System.out.println("线程名称为:" + Thread.currentThread().getName() + "在"
				+ System.currentTimeMillis() + "离开printB");
	}

}
public class ThreadA extends Thread {
	private Service service;

	public ThreadA(Service service) {
		super();
		this.service = service;
	}

	@Override
	public void run() {
		service.printA();
	}
}
public class ThreadB extends Thread {
	private Service service;

	public ThreadB(Service service) {
		super();
		this.service = service;
	}

	@Override
	public void run() {
		service.printB();
	}
}
public static void main(String[] args) {

		Service service1 = new Service();
		Service service2 = new Service();
		System.out.println(service1.getClass() == service2.getClass());
		ThreadA a = new ThreadA(service1);
		a.setName("A");
		a.start();

		ThreadB b = new ThreadB(service2);
		b.setName("B");
		b.start();

	}

public class Service {

	public static void printA() {
		synchronized (Service.class) {
			try {
				System.out.println("线程名称为:" + Thread.currentThread().getName()
						+ "在" + System.currentTimeMillis() + "进入printA");
				Thread.sleep(3000);
				System.out.println("线程名称为:" + Thread.currentThread().getName()
						+ "在" + System.currentTimeMillis() + "离开printA");
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}

	}

	public static void printB() {
		synchronized (Service.class) {
			System.out.println("线程名称为:" + Thread.currentThread().getName()
					+ "在" + System.currentTimeMillis() + "进入printB");
			System.out.println("线程名称为:" + Thread.currentThread().getName()
					+ "在" + System.currentTimeMillis() + "离开printB");
		}
	}
}

两个线程还是上面的不变。

public static void main(String[] args) {

		Service service1 = new Service();
		Service service2 = new Service();

		ThreadA a = new ThreadA(service1);
		a.setName("A");
		a.start();

		ThreadB b = new ThreadB(service2);
		b.setName("B");
		b.start();

	}







猜你喜欢

转载自blog.csdn.net/qq_35400008/article/details/80418376