java线程通讯的方式主要有三种
suspend/resume
- wait/notify
- park/unpark
suspend/resume
- 正常的suspend/resume
public void suspendResumeTest() throws Exception {
Thread consumerThread = new Thread(() -> {
if (baozidian == null) {
System.out.println("1、进入等待");
Thread.currentThread().suspend();
}
System.out.println("2、买到包子,回家");
});
consumerThread.start();
Thread.sleep(3000L);
baozidian = new Object();
consumerThread.resume();
System.out.println("3、通知消费者");
}
- 死锁的suspend/resume: suspend并不会像wait一样释放锁,故此容易写出死锁代码
public void suspendResumeDeadLockTest() throws Exception {
Thread consumerThread = new Thread(() -> {
if (baozidian == null) {
System.out.println("1、进入等待");
synchronized (this) {
Thread.currentThread().suspend();
}
}
System.out.println("2、买到包子,回家");
});
consumerThread.start();
Thread.sleep(3000L);
baozidian = new Object();
synchronized (this) {
consumerThread.resume();
}
System.out.println("3、通知消费者");
}
- 导致程序永久挂起的suspend/resume
public void suspendResumeDeadLockTest2() throws Exception {
Thread consumerThread = new Thread(() -> {
if (baozidian == null) {
System.out.println("1、没包子,进入等待");
try {
Thread.sleep(5000L);
} catch (InterruptedException e) {
e.printStackTrace();
}
Thread.currentThread().suspend();
}
System.out.println("2、买到包子,回家");
});
consumerThread.start();
Thread.sleep(3000L);
baozidian = new Object();
consumerThread.resume();
System.out.println("3、通知消费者");
consumerThread.join();
}
wait/notify
- 正常的wait/notify
public void waitNotifyTest() throws Exception {
new Thread(() -> {
if (baozidian == null) {
synchronized (this) {
try {
System.out.println("1、进入等待");
this.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
System.out.println("2、买到包子,回家");
}).start();
Thread.sleep(3000L);
baozidian = new Object();
synchronized (this) {
this.notifyAll();
System.out.println("3、通知消费者");
}
}
- 会导致程序永久等待的wait/notify
public void waitNotifyDeadLockTest() throws Exception {
new Thread(() -> {
if (baozidian == null) {
try {
Thread.sleep(5000L);
} catch (InterruptedException e1) {
e1.printStackTrace();
}
synchronized (this) {
try {
System.out.println("1、进入等待");
this.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
System.out.println("2、买到包子,回家");
}).start();
Thread.sleep(3000L);
baozidian = new Object();
synchronized (this) {
this.notifyAll();
System.out.println("3、通知消费者");
}
}
park/unpark
- 正常的park/unpark
public void parkUnParkTest() throws Exception {
Thread consumerThread = new Thread(() -> {
if (baozidian == null) {
System.out.println("1、进入等待");
LockSupport.park();
}
System.out.println("2、买到包子,回家");
});
consumerThread.start();
Thread.sleep(3000L);
baozidian = new Object();
LockSupport.unpark(consumerThread);
System.out.println("3、通知消费者");
}
- 死锁的park/unpark
public void parkUnParkDeadLockTest() throws Exception {
Thread consumerThread = new Thread(() -> {
if (baozidian == null) {
System.out.println("1、进入等待");
synchronized (this) {
LockSupport.park();
}
}
System.out.println("2、买到包子,回家");
});
consumerThread.start();
Thread.sleep(3000L);
baozidian = new Object();
synchronized (this) {
LockSupport.unpark(consumerThread);
}
System.out.println("3、通知消费者");
}
总结
- suspend/resume对调用顺序有要求,也要开发自己注意锁的释放。这个被弃用的API, 容易死锁,也容易导致永久挂起。
- wait/notify要求再同步关键字里面使用,免去了死锁的困扰,但是一定要先调用wait,再调用notify,否则永久等待了
- park/unpark没有顺序要求,但是park并不会释放锁,所以在同步代码中使用要特别注意,防止出现死锁