wait

package com.gupaoedu.vip.spring.exam.thread;

/**
 * @author Jly
 * @date 2019/7/30  14:00
 */
public class Service {

    public void mSleep(){
        synchronized(this){
            try{
                Thread.sleep(3*1000);
                System.out.println(" notifyAll唤醒等待前 。 结束时间:"+System.currentTimeMillis());
                this.notifyAll();
                System.out.println(" notifyAll唤醒等待后 。 结束时间:"+System.currentTimeMillis());
            }
            catch(Exception e){
                System.out.println(e);
            }

        }

    }

    public void mWait(){

        synchronized(this){
            try{
                System.out.println(" wait等待开始 。 当前时间:"+System.currentTimeMillis());
                this.wait();
                System.out.println(" wait等待结束 。 当前时间:"+System.currentTimeMillis());
            }catch(Exception e){
                System.out.println(e);
            }
        }

    }

    public static void main(String[] args) {
        Service mService = new Service();

        Thread sleepThread = new Thread(new SleepThread(mService));
        Thread waitThread = new Thread(new WaitThread(mService));
        sleepThread.start();
        waitThread.start();
        //执行结果:
//        wait等待开始 。 当前时间:1564467228874
//        notifyAll唤醒等待前 。 结束时间:1564467231875
//        notifyAll唤醒等待后 。 结束时间:1564467231875
//        wait等待结束 。 当前时间:1564467231875

    }

}
  • 执行结果说明什么,wait没有释放锁,直到notifyAll 唤醒,才执行完下面的代码释放锁
相关文章
相关标签/搜索