多线程 - Java中用CyclicBarrier以及CountDownLatch和join相比有什么不同
迷茫
迷茫 2017-04-17 11:25:06
0
2
774

感觉join就可以解决问题啊,为什么要有CyclicBarrier和CountDownLatch

迷茫
迷茫

业精于勤,荒于嬉;行成于思,毁于随。

reply all(2)
大家讲道理

The first one. Thread.join can only partially implement the function of CountDownLatch. Just look at the java doc of CountDownLatch. Not much to say

The second one. Thread.join uses the Object.wait/notify mechanism. java.lang.Thread.join(long millis) java doc writes:

This implementation uses a loop of this.wait calls conditioned on
this.isAlive. As a thread terminates the this.notifyAll method is
invoked. It is recommended that applications not use wait, notify,
or notifyAll on Thread instances
.

Effective Java 2nd Edition said it very well. Compared with the functions provided by java.util.concurrent, wait/nofify is like concurrency assembly language. It is more difficult to use and easier to use. bug.

To summarize: After the concurrency package is available from java 1.5, the previous wait/notify/timer/synchorinized/synchronized container, etc., should be replaced by the java 1.5 concurrency package as much as possible.

阿神

http://stackoverflow.com/questions/21808814/whats-the-difference-between-cyclicbarrier-countdownlatch-and-join-in-java

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template