Home  >  Q&A  >  body text

多线程 - Java中用CyclicBarrier以及CountDownLatch和join相比有什么不同

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

迷茫迷茫2762 days ago765

reply all(2)I'll reply

  • 大家讲道理

    大家讲道理2017-04-17 11:27:06

    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.

    reply
    0
  • 阿神

    阿神2017-04-17 11:27:06

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

    reply
    0
  • Cancelreply