美文网首页
Object#wait(),notify()

Object#wait(),notify()

作者: Depro | 来源:发表于2021-08-05 19:15 被阅读0次
    1. Object#wait()方法会使调用这个方法的线程释放锁,然后当前调用线程进入waiting状态,直到有另外一个线程调用了这个锁的notify()方法
     @Test
        public void t1() throws InterruptedException {
            final Object lock = new Object();
            final CountDownLatch countDownLatch=new CountDownLatch(2);
            Thread a = new Thread(new Runnable() {
                @Override
                public void run() {
                    System.out.println(Thread.currentThread().getName() + "尝试获取锁");
                    synchronized (lock) {
                        System.out.println(Thread.currentThread().getName() + "拿到了锁");
                        try {
                            //这里Thread-A调用了sleep()方法,但是不会释放它持有的锁lock
                            Thread.sleep(1000);
                            //这个wait()方法会释放锁,等待lock对象调用notify()方法来唤醒继续执行
                            System.out.println(Thread.currentThread().getName() + "调用wait()释放锁,开始等待");
                            lock.wait();
                            System.out.println(Thread.currentThread().getName() + "收到了lock的notify()方法,结束等待");
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                        countDownLatch.countDown();
                    }
                }
            }, "Thread-A");
    
            Thread b = new Thread(new Runnable() {
                @Override
                public void run() {
                    System.out.println(Thread.currentThread().getName() + "尝试获取锁");
                    synchronized (lock) {
                        try {
                            System.out.println(Thread.currentThread().getName() + "获取锁");
                            //这里Thread-B调用了sleep()方法,但是不会释放它持有的锁lock
                            Thread.sleep(1000);
                            System.out.println(Thread.currentThread().getName() + "运行lock.notify()方法进行通知另外一个获取了lock在wait的线程");
                            lock.notify();
                            countDownLatch.countDown();
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                }
            }, "Thread-B");
    
            a.start();
            b.start();
            countDownLatch.await();
        }
    }
    
    • 运行结果:
      结果说明Thread-A调用lock.wait()方法的时候,就释放了锁资源,这个时候Thread-B就立马拿到了锁lock;在Thread-B中调用了同一把锁lock.notify()的时候,Thread-A就唤醒了,继续执行
    Thread-A尝试获取锁
    Thread-B尝试获取锁
    Thread-A拿到了锁
    Thread-A调用wait()释放锁,开始等待
    Thread-B获取锁
    Thread-B运行lock.notify()方法进行通知另外一个获取了lock在wait的线程
    Thread-B运行结束
    Thread-A收到了lock的notify()方法,结束等待
    
    1. Thread#sleep()方法,当前调用方法的线程不会释放锁,只是在休眠等待
    2. Object#wait()方法,会使得持有当前锁对象的线程释放当前锁资源,通过Object#notify()唤醒

    相关文章

      网友评论

          本文标题:Object#wait(),notify()

          本文链接:https://www.haomeiwen.com/subject/kjxvvltx.html