最近在看Java特种兵,看到多线程部分,对wait和notify不是很理解,于是写了代码来帮助理解。特此记录下来,以供回顾。
wait方法通过参数可以指定等待的时长。如果没有指定参数,默认一直等待直到被通知。notify方法是通知某个正在等待这个对象的控制权的线程可以继续运行。
调用wait方法时候,必须加上synchronized同步块,不然会抛出java.lang.IllegalMonitorStateException异常
public class WaitAndNotify {
private final static Object object = new Object();
static class ThreadA extends Thread{
public void run(){
//通过object对象进行加锁
synchronized (object) {
System.out.println(Thread.currentThread().getName()+":start...");
try {
//调用当前对象的wait方法
Thread.currentThread().wait();
} catch (InterruptedException e) {
System.out.println("aaaa");
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName()+":end...");
}
}
}
public static void main(String[] args) throws InterruptedException {
ThreadA t1 = new ThreadA();
t1.setName("t1");
t1.start();
Thread.sleep(100);
System.out.println(t1.getName()+":"+t1.getState());
}
}
这段程序是报异常:
t1:start...
Exception in thread "t1" java.lang.IllegalMonitorStateException
at java.lang.Object.wait(Native Method)
at java.lang.Object.wait(Unknown Source)
at Thread.WaitAndNotify$ThreadA.run(WaitAndNotify.java:15)
t1:TERMINATED
原因是:锁对象和调用wait方法的对象不是同一个。wait方法是Object的方法,对象和调用wait方法的对象必须是同一个对象。
以下是修改后的正确代码:
public class WaitAndNotify {
private final static Object object = new Object();
static class ThreadA extends Thread{
public void run(){
//通过object对象进行加锁
synchronized (this) {
System.out.println(Thread.currentThread().getName()+":start...");
try {
//调用当前对象的wait方法
Thread.currentThread().wait();
} catch (InterruptedException e) {
System.out.println("aaaa");
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName()+":end...");
}
}
}
public static void main(String[] args) throws InterruptedException {
ThreadA t1 = new ThreadA();
t1.setName("t1");
t1.start();
Thread.sleep(100);
System.out.println(t1.getName()+":"+t1.getState());
}
}
或者:
public class WaitAndNotify {
private final static Object object = new Object();
static class ThreadA extends Thread{
public void run(){
//通过object对象进行加锁
synchronized (object) {
System.out.println(Thread.currentThread().getName()+":start...");
try {
//调用object对象的wait方法
object.wait();
} catch (InterruptedException e) {
System.out.println("aaaa");
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName()+":end...");
}
}
}
public static void main(String[] args) throws InterruptedException {
ThreadA t1 = new ThreadA();
t1.setName("t1");
t1.start();
Thread.sleep(100);
System.out.println(t1.getName()+":"+t1.getState());
}
}
上面两段代码虽然正确执行了,但是由于线程状态一直是WAITING,会一直等待下去,这个时候,我们使用调用notify方法,让线程继续执行下去
public class WaitAndNotify {
private final static Object object = new Object();
static class ThreadA extends Thread{
public void run(){
//通过object对象进行加锁
synchronized (object) {
System.out.println(Thread.currentThread().getName()+":start...");
try {
//调用object对象的wait方法
object.wait();
} catch (InterruptedException e) {
System.out.println("aaaa");
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName()+":end...");
}
}
}
public static void main(String[] args) throws InterruptedException {
ThreadA t1 = new ThreadA();
t1.setName("t1");
t1.start();
Thread.sleep(100);
System.out.println(t1.getName()+":"+t1.getState());
//和wait方法一样,需要加锁
synchronized (object) {
object.notify();
}
}
}