package com.njust.test.learn;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * notify需要退出同步代码块再释放锁
 * -------------------------------
 * <pre>
 * [14:14:03] waitTest ==> begin wait
 * [14:14:03] notifyTest ==> begin notify
 * [14:14:03] notifyTest ==> end notify
 * [14:14:06] notifyTest ==> inner sleep end
 * [14:14:06] waitTest ==> end wait
 * [14:14:09] notifyTest ==> outer sleep end
 *
 * Process finished with exit code 0
 * </pre>
 */
public class WaitNotifyDemo222
{
    public static void main(String[] args)
    {
        NotifyTest notifyTest = new NotifyTest();
        
        new Thread(notifyTest::waitTest).start();
        
        new Thread(notifyTest::notifyTest).start();
    }
}

class NotifyTest
{
    public void waitTest()
    {
        synchronized (this)
        {
            try
            {
                printLog("waitTest ==> begin wait");
                this.wait();
                printLog("waitTest ==> end wait");
            }
            catch (InterruptedException e)
            {
                e.printStackTrace();
            }
        }
    }
    
    public void notifyTest()
    {
        synchronized (this)
        {
            printLog("notifyTest ==> begin notify");
            this.notify();
            //1.这里已经调用了notify
            printLog("notifyTest ==> end notify");
            
            try
            {
                Thread.sleep(3000);
                printLog("notifyTest ==> inner sleep end");
                //2.这里才退出同步代码块
            }
            catch (InterruptedException e)
            {
                e.printStackTrace();
            }
        }
        
        try
        {
            Thread.sleep(3000);
            printLog("notifyTest ==> outer sleep end");
            //3.线程继续执行到这儿，不过这里不在同步代码块中
        }
        catch (InterruptedException e)
        {
            e.printStackTrace();
        }
    }
    
    //为了记录时间点
    private void printLog(String msg)
    {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("HH:mm:ss");
        String dateStr = simpleDateFormat.format(new Date());
        System.out.println("[" + dateStr + "] " + msg);
    }
    
}

@@ -100,21 +100,35 @@
 tify
-%E9%9C%80%E8%A6%81
 %E9%80%80%E5%87%BA%E5%90%8C%E6%AD%A5%E4%BB%A3%E7%A0%81%E5%9D%97
-%E5%86%8D
+%E6%89%8D%E4%BC%9A
 %E9%87%8A%E6%94%BE%E9%94%81
+%EF%BC%8C%E8%80%8C%E4%B8%8D%E6%98%AF%E8%B0%83%E7%94%A8notify%E5%B0%B1%E9%87%8A%E6%94%BE
 %0A * 

Update WaitNotifyDemo222.java