第二章、(3)synchronized 同步代码块

摘要:

一、synchronized 代码块中的代码是同步执行,A线程执行完才轮到B线程;不在synchronized代码块中则是异步执行。

二、synchronized(this) 和 synchronized 方法一样,锁定的是当前对象。也就是说:当多个线程同时访问 同一个 object对象的synchronized(this) 方法时,改object 对象中其他synchronized(this) 或者 synchronized 方法都会被阻塞,因为它们使用的是同一个对象锁

1. synchronized同步方法在某些情况下是有弊端的,例如 A线程调用同步方法执行一个长时间任务(方法内有部分代码B线程可以异步调用),B线程则必须等待A线程执行完同步方法才能调用。这种情况下,可以考虑使用 synchronized代码块

public class Task {
	public void doLongTimeTask() throws InterruptedException {
		for (int i = 0; i < 100; i++) {
			System.out.println("nosynchronized threadName=" + Thread.currentThread().getName() + " i=" + (i + 1));
			Thread.sleep(100);
		}
		System.out.println("-----------------------");

		synchronized (this) {
			for (int i = 0; i < 100; i++) {
				System.out.println("synchronized threadName=" + Thread.currentThread().getName() + " i=" + (i + 1));
				Thread.sleep(100);
			}
		}
	}
}
public class MyThread1 extends Thread {
	private Task task;
	public MyThread1(Task task) {
		super();
		this.task = task;
	}
	@Override
	public void run() {
		super.run();
		try {
			task.doLongTimeTask();
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
	}
}
public class MyThread2 extends Thread {
	private Task task;
	public MyThread2(Task task) {
		super();
		this.task = task;
	}
	@Override
	public void run() {
		super.run();
		try {
			task.doLongTimeTask();
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
	}
}
public class Run {
	public static void main(String[] args) {
		Task task = new Task();

		MyThread1 thread1 = new MyThread1(task);
		thread1.start();

		MyThread2 thread2 = new MyThread2(task);
		thread2.start();
	}
}

运行结果: synchronized 代码块中的代码是同步执行,A线程执行完才轮到B线程;不在synchronized代码块中则是异步执行。

第二章、(3)synchronized 同步代码块_第1张图片第二章、(3)synchronized 同步代码块_第2张图片

2. synchronized(this):synchronized(this) 和 synchronized 方法一样,锁定的是当前对象。也就是说:当多个线程同时访问 同一个 object对象的synchronized(this) 方法时,改object 对象中其他synchronized(this) 或者 synchronized 方法都会被阻塞,因为它们使用的是同一个对象锁

public class MyThread1 extends Thread {
	private Task task;
	public MyThread1(Task task) {
		super();
		this.task = task;
	}
	@Override
	public void run() {
		super.run();
		task.doLongTimeTaskA();
	}
}
public class MyThread2 extends Thread {
	private Task task;
	public MyThread2(Task task) {
		super();
		this.task = task;
	}
	@Override
	public void run() {
		super.run();
		task.otherMethod();
	}
}
public class MyThread3 extends Thread {
	private Task task;
	public MyThread3(Task task) {
		super();
		this.task = task;
	}
	@Override
	public void run() {
		super.run();
		task.doLongTimeTaskB();
	}
}
public class Task {
	synchronized public void otherMethod() {
		System.out.println("------------------------run--otherMethod");
	}
	public void doLongTimeTaskA() {
		synchronized (this) {
			for (int i = 0; i < 1000; i++) {
				System.out.println("Task A, synchronized threadName="
						+ Thread.currentThread().getName() + " i=" + (i + 1));
			}
		}
	}
	public void doLongTimeTaskB() {
		synchronized (this) {
			for (int i = 0; i < 1000; i++) {
				System.out.println("Task B, synchronized threadName="
						+ Thread.currentThread().getName() + " i=" + (i + 1));
			}
		}
	}
}
public class Run {
	public static void main(String[] args) throws InterruptedException {
		Task task = new Task();
		MyThread1 thread1 = new MyThread1(task);
		thread1.start();
		Thread.sleep(100);

		MyThread2 thread2 = new MyThread2(task);
		thread2.start();
		MyThread3 thread3 = new MyThread3(task);
		thread3.start();
	}
}

运行结果:synchronized(this)、synchronized方法 持有同一个 object 对象,并且是阻塞运行的。

第二章、(3)synchronized 同步代码块_第3张图片

你可能感兴趣的:(《Java,多线程编程核心技术》笔记)