Countdown Latch

A concept:
1. When multi-threaded collaboration completes business functions, sometimes it is necessary to wait for other threads to complete tasks before the main thread can continue to perform business functions. In this business scenario, the join method of Thread class is usually used to let the main thread wait for the thread joined to finish executing before the main thread can continue. Go ahead.
Of course, the use of inter-thread message communication mechanism can also be completed. In fact, the java Concurrent tool class provides us with such a tool class as "countdown", which is very convenient to complete the business scenario.

2. To understand CountDownLatch, give a popular example:

There are six athletes and one referee. The athlete must wait for the referee's gunshot to start the competition. At the same time, the referee must wait until the athletes are ready to start the competition. The referee will time the six athletes separately at the end of the competition. It can be imagined that when an athlete reaches the end, he will come to the referee. That's a missing timekeeping task. It was not until all the athletes reached the end that the referee's task was completed. These six athletes can be analogized to six threads.

public class CountDownLatchDemo {

	private static CountDownLatch readySignal = new CountDownLatch(6);  // Indicate whether six athletes are ready
	private static CountDownLatch startSignal = new CountDownLatch(1);  // The referee fired a match shot.
	private static CountDownLatch endSignal = new CountDownLatch(6);  // Indicates whether six athletes have reached the finish line

	public static void main(String[] args) throws InterruptedException {
		ExecutorService executorService = Executors.newFixedThreadPool(6);
		for (int i = 0; i < 6; i++) {
			executorService.execute(() -> {
				try {
					System.out.println(Thread.currentThread().getName() + " Athletes wait for referees to whistle!!!");
					System.out.println(Thread.currentThread().getName() + " Athletes ready to complete!!!");
					readySignal.countDown();
					readySignal.await();
					startSignal.await();
					System.out.println(Thread.currentThread().getName() + " Sprint in full swing");
					System.out.println(Thread.currentThread().getName() + " Arrive at the End");
					endSignal.countDown();
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			});
		}
		readySignal.await();  // Waiting for all the athletes to be ready
		System.out.println("All athletes are ready to finish!!! The referee gave orders!!!");
		startSignal.countDown();
		endSignal.await();
		System.out.println("All athletes arrive at the end of the game!");
		executorService.shutdown();
	}
}

Operation results:

The pool-1-thread-1 athlete waits for the referee to whistle!!!
The pool-1-thread-2 player waits for the referee to whistle!!!
The pool-1-thread-2 athletes are ready to finish!!!
The pool-1-thread-3 player waits for the referee to whistle!!!
The pool-1-thread-1 athlete is ready to finish!!!
The pool-1-thread-3 athletes are ready to finish!!!
The pool-1-thread-4 player waits for the referee to whistle!!!
The pool-1-thread-4 athletes are ready to finish!!!
The pool-1-thread-5 athlete waits for the referee to whistle!!!
The pool-1-thread-5 athletes are ready to finish!!!
The pool-1-thread-6 player waits for the referee to whistle!!!
The pool-1-thread-6 athletes are ready to finish!!!
All athletes are ready to finish!!! The referee gave orders!!!
pool-1-thread-6 is sprinting at full speed
 pool-1-thread-4 is sprinting at full speed
 pool-1-thread-6 arrives at the destination
 pool-1-thread-5 is sprinting at full speed
 pool-1-thread-5 arrives at the destination
 pool-1-thread-3 is sprinting at full speed
 pool-1-thread-1 is sprinting at full speed
 pool-1-thread-1 arrives at the destination
 pool-1-thread-2 is sprinting at full speed
 pool-1-thread-2 arrives at the destination
 pool-1-thread-3 arrives at the destination
 pool-1-thread-4 arrives at the destination
 All athletes arrive at the end of the game!

Process finished with exit code 0

Keywords: Java

Added by james13009 on Fri, 02 Aug 2019 12:50:59 +0300