wait-notify和CountDownLatch之间的差异 [英] Difference between wait-notify and CountDownLatch

查看:344
本文介绍了wait-notify和CountDownLatch之间的差异的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要一些帮助理解使用CountDownLatch的优势,而不是传统的等待通知。我想notifyAll()的确做同样的事情,它似乎更容易使用(也许是因为熟悉)。



还有,wait()和await ()from CountDownLatch?



谢谢!



编辑:我想我需要重新整理我的查询: p>

根据文档,Await():


使当前线程<


对于我来说很难看到wait()和await()之间的区别 - await()确实是使用wait()覆盖,似乎有一个隐含的notifyAll()当计数达到零时。



我的意思是,为什么我不应该使用wait-notifyAll()机制(使用我自己的计数器变量处理),而不是去CountDownLatch?

解决方案

他们当然不会做同样的事情: CountDownLatch 当事件计数已经达到0并且它自动这样做的信号, wait-notify 要求你保持自己的计数,如果你想实现相同的行为。实现相同的行为往往容易出错,最好是避免它(尤其是如果你是并发编程的新手)。比较 CountDownLatch wait-notify 几乎不是一个苹果对橘子的比较,它更像是比较一个自动钻取和Allen扳手。



我不知道你是否使用过 notifyAll() CountDownLatch ,但 notifyAll()单独不会给你相同的行为,除非你已经计数了发生了多少事件。 CountDownLatch 可能最适合执行固定数量的任务,并等待这些任务完成,然后再恢复执行剩余的程序。当你有固定数量的线程(例如 ThreadPool )执行固定数量的任务时,这是特别有用,但是你的线程比任务少,你必须重用它们。使用 CountDownLatch ,您可以轻松地等待所有任务完成。我不知道你是如何使用 notifyAll()来实现相同的行为,但是如果你提供更多的信息,我们可以解决两者之一一个更好的选择(肯定有一些情况下 waitNotify()更合适)。



wait() await(),我有点失望!查找文档是任何问题的第一步:



http://docs.oracle.com/javase/7/docs/api/java/util/concurrent/CountDownLatch.html



await() CountDownLatch 的实际函数, wait )继承自 Object 。我建议您检查文档中的操作。


I need some help understanding the advantages of using CountDownLatch over traditional wait-notify. I think notifyAll() indeed does the same thing, and it seems easier to use (maybe because of familiarity).

Also, what's the difference between wait() and await() from CountDownLatch ?

Thanks !

EDIT : I guess I need to rephrase my queries :

Await() as per the docs says :

Causes the current thread to wait until the latch has counted down to zero, unless the thread is interrupted.

For me it's hard to see the difference between wait() and await() - await() is indeed using wait() under covers, and seems there is an implicit notifyAll() when count reached zero.

What I meant to ask was, why shouldn't I simply use a wait-notifyAll() mechanism (with my own counter variable processing), rather than going for CountDownLatch ?

解决方案

They certainly don't do the same thing: CountDownLatch only signals when the event count has reached 0 and it does so automatically, wait-notify requires you to keep your own count if you want to achieve the same behavior. Implementing the same behavior is often error prone and it's best that you avoid it (especially if you're new to concurrency programming). Comparing CountDownLatch and wait-notify is hardly even an apples to oranges comparison, it's more like comparing an automatic drill and an Allen wrench.

I don't know if you've used notifyAll() and CountDownLatch, but notifyAll() alone will not give you the same behavior unless you've kept count of how many events have occurred. CountDownLatch is probably most suitable for performing a fixed number of tasks and waiting for those tasks to complete before you resume execution of the rest of your program. It's especially helpful when you have a fixed number of threads (e.g. ThreadPool) executing a fixed number of tasks, but your threads are way fewer than the tasks and you have to reuse them. With a CountDownLatch you can easily wait for all of the tasks to be completed. I don't know how you've been using notifyAll() to achieve the same behavior, but if you provide us with more information we can address which one of the two is a better choice (there are certainly some cases where waitNotify() is more appropriate).

Regarding the difference between wait() and await(), I'm somewhat disappointed in you! Looking up the documentation is step one of any question:

http://docs.oracle.com/javase/7/docs/api/java/util/concurrent/CountDownLatch.html

await() is an actual function of CountDownLatch whereas wait() is inherited from Object. I would recommend that you check the documentation for what they do.

这篇关于wait-notify和CountDownLatch之间的差异的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆