Utility class for thread concurrency: CountDownLatch
-
CountDownLatch: atresia
-
Summary:
-
One or more threads wait for the other threads to complete their work before executing
-
Application scenario: for example, starting a framework, putting data initialization into a single thread/thread, the main thread, etc. After these threads finish executing, the execution (putting initialization work in the main thread affects performance)
-
Internal mechanism:
- The worker thread calls, with await() inside, and waits for the “initialization” thread to complete;
- It contains a counter (CNT, which normally has an initial value) that, once one of the “initialization” threads has completed (or a part of it has completed), calls countDown() to subtract 1 from the CNT,
- When CNT is 0, the worker thread continues execution
-
Execution process:
-
Implementation details:
-
The CNT has nothing to do with the initialization thread. How many times can a thread do it
-
Initialization thread at CNT 0, this thread can continue to execute
- Just make a thread pool, initialize it and do something else
-
There can be more than one waiting thread
-
Note that the CNT setup, if not completed –> the business thread is blocked
-
-
-
Code implementation:
package cn.enjoyedu.ch2.tools; import java.util.concurrent.CountDownLatch; import cn.enjoyedu.tools.SleepTools; /** * To demonstrate the use of CountDownLatch, * there are 5 initial child threads and 6 latching points. */ public class UseCountDownLatch {//TODO // static CountDownLatch = new CountDownLatch(6); Private static class implements Runnable{@override public void run() {private static class implements Runnable{@override public void run() { System.out.println("Thread_"+Thread.currentThread().getId() +" ready init work......" ); // when the initializer thread completes an initialization operation, the CNT decreases by one; You can actually decrement anything you want, decrement to 0 means that the initialization is done latch.countdown (); for(int i =0; i<2; i++) { System.out.println("Thread_"+Thread.currentThread().getId() +" ........ continue do its work"); }}} // Business thread: Private static class BusiThread implements Runnable{@override public void run() {// Meaning initialization is complete, try {latch.await(); } catch (InterruptedException e) { e.printStackTrace(); } for(int i =0; i<3; i++) { System.out.println("BusiThread_"+Thread.currentThread().getId() +" do business-----"); } } } public static void main(String[] args) throws InterruptedException { new Thread(new Runnable() { @Override public void run() { SleepTools.ms(1); System.out.println("Thread_"+Thread.currentThread().getId() +" ready init work step 1st......" ); Latch.countdown (); latch.countdown (); System.out.println("begin step 2nd......." ); SleepTools.ms(1); System.out.println("Thread_"+Thread.currentThread().getId() +" ready init work step 2nd......" ); // Latch.countdown (); } }).start(); new Thread(new BusiThread()).start(); for(int i=0; i<=3; i++){ Thread thread = new Thread(new InitThread()); thread.start(); } // The main thread can also wait, meaning waiting threads can have multiple latches. Await (); System.out.println("Main do ites work........" ); }}Copy the code
-
Run screenshot:
-