Java CyclicBarrier, CountDownLatch and wait () What is the difference?

table of Contents


1. Problem

  • CyclicBarrier, CountDownLatch and wait () What is the difference?

2. Answers

  • wait(): Object class method is provided that allows the current thread to stop executing, and release the resources associated with the conversion from a running thread state to wait state;
  • CyclicBarrier(int n, Runnable barrierAction): CyclicBarrier class constructor is used when n threads have reached the barrier, the following thread execution priority barrierAction, then other threads to execute, easy to handle more complex business scenario;
  • CountDownLatch(int count): Is the class constructor CountDownLatch, refers to half a thread execution is interrupted, wait for the end of the other n threads running in order to continue the thread of the code;
  • About using wait () method, see: the Java thread interaction ;
  • CountDownLatch and on the use of CyclicBarrier, see: the Java and contract ;
He published 189 original articles · won praise 189 · Views 5984

Guess you like

Origin blog.csdn.net/Regino/article/details/104737259