86. Lock CountDownLatch

Question description

Question: jdk5.0 provides CountDownLatch in the java.util.concurrent package, which is a synchronization auxiliary class that allows one or more threads to wait until a set of operations being performed in other threads is completed.

Locking can delay the progress of a thread until it reaches the termination state. Locking can be used to ensure that certain activities do not continue execution until other activities have completed:

1) Ensure that a calculation continues execution after all resources it requires have been initialized;

2) Ensure that a service is started only after all other dependent services have been started;

3) Wait until all participants of an operation are ready to continue execution.

Implementation: Use CountDownLatch to implement a method that waits for 5 tasks to be completed before proceeding with the following tasks.

Problem-solving ideas

CountDownLatch provides an await method, which is used to wait for the threads counted by the previous countdownlatch to complete before executing downwards, otherwise it will wait forever.

Create a class: Question86

In the main method, new CountDownLatch(5) initializes the number of open counts. When it is 0, it will jump out of waiting.

Get the current timestamp

Long start 

Guess you like

Origin blog.csdn.net/weixin_43344151/article/details/125269661