Graphical rules of the game
We are not talking about relays, we are talking about “relay collaboration”, we need to redefine the rules of the game: as shown in the picture below
Now player A and player B define the rules of the game: Track is 300 meters, each athlete when run the first 100 meters, need to wait for other athletes run the first 100 meters, such as A first run the 100 metres athletes, while player B only made it about 95 meters, the athletes have to wait for the athlete B run the remaining 5 meters, and then together then run 2 100 meters, 100 meters of the third, The rules are similar to the first 100 meters, and at the end we can come to a conclusion as to how long it will take two runners to complete the 300 meters. This case is purely fictitious, in order to clarify CyclicBarrier. Let’s simulate the execution with code.
Case description
import java.util.concurrent.BrokenBarrierException; import java.util.concurrent.CyclicBarrier; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; /** * @author: jiaolian * @date: Created in 2021-03-01 14:56 * @description: Modified By: CyclicBarrierTest {private static final int THREAD_COUNT = 2; CyclicBarrierTest {private static final int THREAD_COUNT = 2; private static CyclicBarrier cyclicBarrier = new CyclicBarrier(2,()->{ System.out.println(thread.currentThread ().getName()+" break barrier "); }); private static ExecutorService executorService = Executors.newFixedThreadPool(THREAD_COUNT); public static void main(String[] args) { Runnable myTask = new MyTask(); For (int I =0; i<THREAD_COUNT; i++) { executorService.submit(myTask); } } private static class MyTask implements Runnable { @Override public void run() { try { System.out.println(thread.currentThread ().getName()+" 100 "); cyclicBarrier.await(); System.out.println(thread.currentThread ().getName()+" 100 "); cyclicBarrier.await(); System.out.println(thread.currentThread ().getName()+" 100m "); cyclicBarrier.await(); } catch (InterruptedException e) { e.printStackTrace(); } catch (BrokenBarrierException e) { e.printStackTrace(); }}}}Copy the code
As shown in the code above: The thread pool simulates the execution of two athletes, and each athlete has to wait for another athlete after completing each 100m. The execution result is consistent with our expectation, as shown in the figure below. Pool-1-thread-1 and pool-1-thread-2 indicate athlete A and athlete B respectively. One of the CyclicBarrier initialization parameters has a Runnable that is used to break barrier callbacks.
Compare CountDownLatch
CyclicBarrier ** : for each thread that calls await, the counter is reduced by 1. If the counter is not 0, the thread will block. If the counter is 0, it will need to break the barrier, wake up the previously blocked thread and reset the counter. The source code implementation uses exclusive locks and conditional queues to control the queue entry and exit of threads. CountDownLatch uses shared locks. Although the implementation is different, the underlying latch is AQS, and compared with CountDownLatch, CyclicBarrier is its complement and more powerful.
conclusion
Today we introduce CyclicBarrier, sort out the hope can help you, write than complete, at the same time there are many places that need to be corrected, I hope you can correct and comment, like, please click on the like and pay attention to oh. ** Point attention, do not get lost, I am [jiaolian123ABC] public account, micro signal [jiaolian123ABC] ** while calling while practicing.