Use example of countdownlatch synchronization tool for Java multithreaded programming

Like the countdown counter, call the countdown method of the countdownlatch object to reduce the counter by 1. When it reaches 0, all waiters begin to execute.

java. util. concurrent. Countdownlatch is a synchronization helper class that allows one or more threads to wait until a set of operations are completed in other threads. Initializes countdownlatch with the given count. Because the countdown () method is called, the await method is blocked until the current count reaches zero. After that, all waiting threads will be released and all subsequent calls to await will return immediately. This happens only once - the count cannot be reset. If you need to reset the count, consider using cyclicbarrier.

Countdownlatch is a general-purpose synchronization tool that has many uses. Use countdownlatch initialized by count 1 as a simple on / off latch or entry: all threads calling await wait at the entry until the entry is opened by the thread calling countdown(). Countdownlatch initialized with n can make a thread wait until n threads complete an operation, or make it wait until an operation is completed n times.

A useful feature of countdownlatch is that it does not require the thread calling the countdown method to wait until the count reaches zero. Before all threads can pass, it just prevents any thread from continuing to pass an await.

For example: multiple athletes wait for the referee's order: the referee will release the results after all athletes arrive

Return result:

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>