Semaphore semaphore, CountdownLatch counting wait

1.semaphore

A semaphore is used to limit the upper limit of threads that can simultaneously access shared resources.

 2.CountdownLatch

Used for thread synchronization and cooperation, waiting for all threads to complete the countdown.

Among them, the construction parameter is used to initialize the waiting count value, await() is used to wait for the count to return to zero (if the count does not return to zero, it will always block), and countDown() is used to decrease the count by 1.

The join() method can also be implemented to wait for several other threads to end before the main thread executes.

And CountdownLatch can be used in the thread pool (the core thread in the thread pool will not end running)

 

Guess you like

Origin blog.csdn.net/weixin_54401017/article/details/127229191