- 创建两个线程,线程一无限等待,线程二唤醒线程一 ```java package thread;
public class WaitSleepDemo { public static void main(String[] args) { final Object lock = new Object(); new Thread(new Runnable() { @Override public void run() { System.out.println(“thread A is waiting to get lock”); synchronized (lock) { try { System.out.println(“thread A get lock”); Thread.sleep(20); System.out.println(“thread A do wait method”); lock.wait(); System.out.println(“thread A is done”); } catch (InterruptedException e) { e.printStackTrace(); } } } }).start(); try { Thread.sleep(10); } catch (InterruptedException e) { e.printStackTrace(); } new Thread(new Runnable() { @Override public void run() { System.out.println(“thread B is waiting to get lock”); synchronized (lock) { try { System.out.println(“thread B get lock”); System.out.println(“thread B is sleeping 10 ms”); Thread.sleep(20); System.out.println(“thread B is done”); // lock.notify(); lock.notifyAll(); } catch (InterruptedException e) { e.printStackTrace(); } } } }).start(); } }
<a name="J4l9j"></a># 7.7.1 锁池 EntryList假设线程 A 已经拥有了某个对象(不是类)的锁,而其他线程 B、C 想要调用这个对象的某个 synchronized 方法(或者块),由于 B、C 线程在进入对象的 synchronized 方法(或者块)之前必须先获得该对象锁的拥有权,而恰巧该对象的锁目前正被线程 A 所占用,此时 B、C 线程就会被阻塞,进入一个地方去等待锁的释放,这个地方便是该对象的锁池。<a name="UklED"></a># 7.7.2 等待池 WaitSet假设线程 A 调用了某个对象的 wait() 方法,线程 A 就会释放该对象的锁,同时线程 A 就进入到了该对象的等待池中,进入到等待池中的线程不会去竞争该对象的锁。<a name="8ukak"></a># 7.7.3 notify 和 notifyAll 的区别- notifyAll 会让所有处于等待池的线程全部进入锁池去竞争获取锁的机会<br />没有获取到锁的而已经待在锁池中的线程只能等待其他机会获取锁,不能主动回到等待池当中- notify 只会随机选取一个处于等待池中的线程进入锁池去竞争获取锁的机会下面看一个示例```javapackage thread;public class NotificationDemo {// 有多个线程进行修改,一旦线程 A 对它进行改动,线程 B、C、D 能立即看到相关的改动private volatile boolean go = false;public static void main(String args[]) throws InterruptedException {final NotificationDemo test = new NotificationDemo();Runnable waitTask = new Runnable() {@Overridepublic void run() {try {test.shouldGo();} catch (InterruptedException e) {e.printStackTrace();}System.out.println(Thread.currentThread().getName() + " finished Execution");}};Runnable notifyTask = new Runnable() {@Overridepublic void run() {test.go();System.out.println(Thread.currentThread().getName() + " finished Execution");}};Thread t1 = new Thread(waitTask, "WT1"); //will waitThread t2 = new Thread(waitTask, "WT2"); //will waitThread t3 = new Thread(waitTask, "WT3"); //will waitThread t4 = new Thread(notifyTask, "NT1"); //will notify//starting all waiting threadt1.start();t2.start();t3.start();//pause to ensure all waiting thread started successfullyThread.sleep(200);//starting notifying threadt4.start();}/** wait and notify can only be called from synchronized method or bock*/private synchronized void shouldGo() throws InterruptedException {while (go != true) {System.out.println(Thread.currentThread()+ " is going to wait on this object");wait(); //release lock and reacquires on wakeupSystem.out.println(Thread.currentThread() + " is woken up");}go = false; //resetting condition}/** both shouldGo() and go() are locked on current object referenced by "this" keyword*/private synchronized void go() {while (go == false) {System.out.println(Thread.currentThread()+ " is going to notify all or one thread waiting on this object");go = true; //making condition true for waiting threadnotify(); // only one out of three waiting thread WT1, WT2,WT3 will woke up// notifyAll(); // all waiting thread WT1, WT2,WT3 will woke up}}}

此处可以看出 notify 只是通知其中一个线程去启动,别的线程仍然处于等待当中。
package thread;
public class NotificationDemo {
// 有多个线程进行修改,一旦线程 A 对它进行改动,线程 B、C、D 能立即看到相关的改动
private volatile boolean go = false;
public static void main(String args[]) throws InterruptedException {
final NotificationDemo test = new NotificationDemo();
Runnable waitTask = new Runnable() {
@Override
public void run() {
try {
test.shouldGo();
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName() + " finished Execution");
}
};
Runnable notifyTask = new Runnable() {
@Override
public void run() {
test.go();
System.out.println(Thread.currentThread().getName() + " finished Execution");
}
};
Thread t1 = new Thread(waitTask, "WT1"); //will wait
Thread t2 = new Thread(waitTask, "WT2"); //will wait
Thread t3 = new Thread(waitTask, "WT3"); //will wait
Thread t4 = new Thread(notifyTask, "NT1"); //will notify
//starting all waiting thread
t1.start();
t2.start();
t3.start();
//pause to ensure all waiting thread started successfully
Thread.sleep(200);
//starting notifying thread
t4.start();
}
/*
* wait and notify can only be called from synchronized method or bock
*/
private synchronized void shouldGo() throws InterruptedException {
while (go != true) {
System.out.println(Thread.currentThread()
+ " is going to wait on this object");
wait(); //release lock and reacquires on wakeup
System.out.println(Thread.currentThread() + " is woken up");
}
go = false; //resetting condition
}
/*
* both shouldGo() and go() are locked on current object referenced by "this" keyword
*/
private synchronized void go() {
while (go == false) {
System.out.println(Thread.currentThread()
+ " is going to notify all or one thread waiting on this object");
go = true; //making condition true for waiting thread
// notify(); // only one out of three waiting thread WT1, WT2,WT3 will woke up
notifyAll(); // all waiting thread WT1, WT2,WT3 will woke up
}
}
}

可以看到所有的等待线程都被唤醒了。
