await & signal & signalAll

Condition

java.util.concurrent类库中提供Condition类来实现线程之间的协调。

调用Condition.await() 方法使线程等待,其他线程调用Condition.signal() 或 Condition.signalAll() 方法唤醒等待的线程。

public class TestObject {
    private Lock lock = new ReentrantLock();
    private Condition condition = lock.newCondition();

    public void first() {
        System.out.println("first is lock");
        lock.lock();
        try {
            System.out.println("first is running");
            condition.signalAll();
        } finally {
            System.out.println("first is unlock");
            lock.unlock();
        }
    }

    public void second() {
        System.out.println("second is lock");
        lock.lock();
        try {
            condition.await();
            System.out.println("second is running");
        } catch (InterruptedException e) {
            e.printStackTrace();
        } finally {
            System.out.println("second is unlock");
            lock.unlock();
        }
    }

    public void third() {
        System.out.println("third is lock");
        lock.lock();
        try {
            condition.await();
            System.out.println("third is running");
        } catch (InterruptedException e) {
            e.printStackTrace();
        } finally {
            System.out.println("third is unlock");
            lock.unlock();
        }
    }
}
public class TestRun {

    private final static ExecutorService executorService = Executors.newCachedThreadPool();

    public static void main(String[] arg) {
        TestObject to = new TestObject();
        executorService.execute(() -> to.third());
        executorService.execute(() -> to.second());
        executorService.execute(() -> to.first());
    }
}
  • 输出结果
third is lock
second is lock
first is lock
first is running
first is unlock
third is running
third is unlock
second is running
second is unlock

备注:上述实例中,若first方法中改为condition.signal()则second不会被唤醒,需要在third方法的finally中增加condition.signal()才能唤醒second。

wait() & await()

  • wait()是Object类中的方法,await()是Condition类里面的方法
  • await的阻塞需要Lock中的Condition对象来触发
  • await会导致当前线程被阻塞,会释放锁,这点和wait是一样的
  • await使用signal唤醒;wait使用notify
  • await只能用于ReentrantLock锁中,wait一般用于Synchronized中

原文 

https://segmentfault.com/a/1190000023131092

本站部分文章源于互联网,本着传播知识、有益学习和研究的目的进行的转载,为网友免费提供。如有著作权人或出版方提出异议,本站将立即删除。如果您对文章转载有任何疑问请告之我们,以便我们及时纠正。

PS:推荐一个微信公众号: askHarries 或者qq群:474807195,里面会分享一些资深架构师录制的视频录像:有Spring,MyBatis,Netty源码分析,高并发、高性能、分布式、微服务架构的原理,JVM性能优化这些成为架构师必备的知识体系。还能领取免费的学习资源,目前受益良多

转载请注明原文出处:Harries Blog™ » await & signal & signalAll

赞 (0)
分享到:更多 ()

评论 0

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址