1. 创建自定义WaitNotify类,实现Runnable接口
public class WaitNotify implements Runnable {
private boolean flag;
private Object object;
public WaitNotify(boolean flag, Object object) {
this.flag = flag;
this.object = object;
}
@Override
public void run() {
if (flag) {
try {
synchronized (object) {
System.out.println(Thread.currentThread().getName()+" sleep");
object.wait();
System.out.println(Thread.currentThread().getName()+" run");
}
} catch (InterruptedException e) {
e.printStackTrace();
}
} else {
synchronized (object) {
object.notifyAll();
}
}
}
}
2. 然后创建一个测试类ThreadPoolTest
public class ThreadPoolTest {