1. 创建两个线程,线程一无限等待,线程二唤醒线程一 ```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(); } }

    1. ![image.png](https://cdn.nlark.com/yuque/0/2021/png/390086/1617622298997-26cb3e2f-e210-455f-9a11-a78dc1f87745.png#align=left&display=inline&height=226&margin=%5Bobject%20Object%5D&name=image.png&originHeight=226&originWidth=235&size=9800&status=done&style=none&width=235)
    2. <a name="J4l9j"></a>
    3. # 7.7.1 锁池 EntryList
    4. 假设线程 A 已经拥有了某个对象(不是类)的锁,而其他线程 B、C 想要调用这个对象的某个 synchronized 方法(或者块),由于 B、C 线程在进入对象的 synchronized 方法(或者块)之前必须先获得该对象锁的拥有权,而恰巧该对象的锁目前正被线程 A 所占用,此时 B、C 线程就会被阻塞,进入一个地方去等待锁的释放,这个地方便是该对象的锁池。
    5. <a name="UklED"></a>
    6. # 7.7.2 等待池 WaitSet
    7. 假设线程 A 调用了某个对象的 wait() 方法,线程 A 就会释放该对象的锁,同时线程 A 就进入到了该对象的等待池中,进入到等待池中的线程不会去竞争该对象的锁。
    8. <a name="8ukak"></a>
    9. # 7.7.3 notify 和 notifyAll 的区别
    10. - notifyAll 会让所有处于等待池的线程全部进入锁池去竞争获取锁的机会<br />没有获取到锁的而已经待在锁池中的线程只能等待其他机会获取锁,不能主动回到等待池当中
    11. - notify 只会随机选取一个处于等待池中的线程进入锁池去竞争获取锁的机会
    12. 下面看一个示例
    13. ```java
    14. package thread;
    15. public class NotificationDemo {
    16. // 有多个线程进行修改,一旦线程 A 对它进行改动,线程 B、C、D 能立即看到相关的改动
    17. private volatile boolean go = false;
    18. public static void main(String args[]) throws InterruptedException {
    19. final NotificationDemo test = new NotificationDemo();
    20. Runnable waitTask = new Runnable() {
    21. @Override
    22. public void run() {
    23. try {
    24. test.shouldGo();
    25. } catch (InterruptedException e) {
    26. e.printStackTrace();
    27. }
    28. System.out.println(Thread.currentThread().getName() + " finished Execution");
    29. }
    30. };
    31. Runnable notifyTask = new Runnable() {
    32. @Override
    33. public void run() {
    34. test.go();
    35. System.out.println(Thread.currentThread().getName() + " finished Execution");
    36. }
    37. };
    38. Thread t1 = new Thread(waitTask, "WT1"); //will wait
    39. Thread t2 = new Thread(waitTask, "WT2"); //will wait
    40. Thread t3 = new Thread(waitTask, "WT3"); //will wait
    41. Thread t4 = new Thread(notifyTask, "NT1"); //will notify
    42. //starting all waiting thread
    43. t1.start();
    44. t2.start();
    45. t3.start();
    46. //pause to ensure all waiting thread started successfully
    47. Thread.sleep(200);
    48. //starting notifying thread
    49. t4.start();
    50. }
    51. /*
    52. * wait and notify can only be called from synchronized method or bock
    53. */
    54. private synchronized void shouldGo() throws InterruptedException {
    55. while (go != true) {
    56. System.out.println(Thread.currentThread()
    57. + " is going to wait on this object");
    58. wait(); //release lock and reacquires on wakeup
    59. System.out.println(Thread.currentThread() + " is woken up");
    60. }
    61. go = false; //resetting condition
    62. }
    63. /*
    64. * both shouldGo() and go() are locked on current object referenced by "this" keyword
    65. */
    66. private synchronized void go() {
    67. while (go == false) {
    68. System.out.println(Thread.currentThread()
    69. + " is going to notify all or one thread waiting on this object");
    70. go = true; //making condition true for waiting thread
    71. notify(); // only one out of three waiting thread WT1, WT2,WT3 will woke up
    72. // notifyAll(); // all waiting thread WT1, WT2,WT3 will woke up
    73. }
    74. }
    75. }

    image.png
    此处可以看出 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
            }
        }
    }
    

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