wait()和notify()方法都是Object方法,这两个方法都必须搭配synchronized使用。
必须拿到相应的锁才能调用,否则抛出IllegalMonitorStateException
。
wait()方法
痴汉方法。持有锁的线程调用wait()方法后会一直阻塞,直到有别的线程调用notify()将其唤醒。
public class TestWait {
public static void main(String[] args) {
Object object = new Object();
synchronized (object){
try {
System.out.println("等待中...");
object.wait();
System.out.println("等待已过...");
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println("执行main方法");
}
}
以上结果:输出 等待中… 就一直在阻塞。
另外还有一个方法:public final native void wait(long timeout):等待一段时间,若还未被唤醒,继续执行,默认单位为毫秒。
notify()方法
唤醒一个处于wait的线程,只能唤醒一个。
class Task implements Runnable{
private boolean flag;
private Object obj;
public Task(boolean flag, Object obj) {
this.flag = flag;
this.obj = obj;
}
@Override
public void run() {
if(flag){
waitTest();
}else{
notifyTest();
}
}
public void waitTest(){
synchronized (obj){
try {
System.out.println(Thread.currentThread().getName()+" 执行wait方法");
obj.wait();
System.out.println(Thread.currentThread().getName()+" wait方法结束");
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public void notifyTest(){
synchronized (obj){
System.out.println(Thread.currentThread().getName()+" 执行notify方法");
obj.notify();
System.out.println(Thread.currentThread().getName()+" notify方法结束");
}
}
}
public class TestThread {
public static void main(String[] args) throws InterruptedException {
Object obj = new Object();
Task task = new Task(true,obj);
Task task1 = new Task(false,obj);
Thread thread = new Thread(task,"wait线程");
Thread thread1 = new Thread(task1,"notify线程");
thread.start();
Thread.sleep(1000); //保证先执行wait后它被唤醒
thread1.start();
}
}
结果表示:只有当notify方法执行结束后才会执行wait后的语句。
首先启动wait线程,获取obj的Monitor,此时执行wait语句,线程阻塞,并释放锁。当启动notify线程时,重新获取锁,执行notify方法,全部执行完后,唤醒wait线程,wait线程重新获取锁。
分析其原因:
任意一个Object及其子类对象,都有2个队列:
- 同步队列:所有尝试获取该对象Monitor失败的线程,都加入同步队列,排队获取锁。
- 等待队列:已经拿到锁的线程,在等待其他资源时,主动释放锁,置入该对象的等待队列中,等待被唤醒。
当调用notify(),会在等待队列中任意唤醒一个线程,将其从等待队列中置入同步队列尾部,排队获取锁。
如果有多个线程在等待,那么可以使用notifyAll方法一次唤醒所有等待线程。
举例:使用notifyAll()
class Task implements Runnable{
private boolean flag;
private Object obj;
public Task(boolean flag, Object obj) {
this.flag = flag;
this.obj = obj;
}
@Override
public void run() {
if(flag){
waitTest();
}else{
notifyTest();
}
}
public void waitTest(){
synchronized (obj){
try {
System.out.println(Thread.currentThread().getName()+" 执行wait方法");
obj.wait();
System.out.println(Thread.currentThread().getName()+" wait方法结束");
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public void notifyTest(){
synchronized (obj){
System.out.println(Thread.currentThread().getName()+" 执行notify方法");
obj.notifyAll();
System.out.println(Thread.currentThread().getName()+" notify方法结束");
}
}
}
public class TestThread {
public static void main(String[] args) throws InterruptedException {
Object obj = new Object();
Task task = new Task(true,obj);
Task task1 = new Task(false,obj);
Thread thread = new Thread(task,"wait线程A");
Thread thread1 = new Thread(task,"wait线程B");
Thread thread2 = new Thread(task,"wait线程C");
Thread thread3 = new Thread(task1,"notify线程");
thread.start();
thread1.start();
thread2.start();
Thread.sleep(1000);
thread3.start();
}
}