1, waiting for multi-threaded completion of the Countdownlatch
Countdownlatch allows one or more threads to wait for another thread to complete the operation. Join is used to let the current thread of execution wait for the join thread to finish. The principle is to keep checking to see if the join thread is alive and let the current thread wait forever if the join thread survives. The This.notifyall () method of the thread is called until the join thread aborts, and the call to Notifyall () method is implemented in the JVM.
2. Synchronous barrier Cyclicbarrier
Cylicbarrier literally means a barrier (Barrier) that can be recycled (Cyclic). It blocks a set of threads from reaching a barrier (also called a synchronization point) until the last thread reaches the barrier and the barrier opens, and all the threads that are blocked by the barrier continue to run.
Countdownlatch counters can only be used once, while cyclicbarrier counters may be reset using the rest () method. So cyclicbarrier can handle more complex business scenarios.
3, control the number of concurrent threads semaphore
Semaphore (semaphore) is the number of threads used to control access to a particular resource at the same time, by coordinating individual threads to ensure reasonable use of public resources.
Semaphore can be used for flow control , especially for applications with limited public resources, such as database connections.
Semaphore usage: The thread first uses the semaphore acquire () method to obtain a license, and then calls the release () method to return the license after use. You can also use the Tryacquire () method to try to obtain a license.
4. Exchanger of exchanging data between threads
Exchanger (the Exchange) is a tool class for collaboration between threads. Exchanger provides a synchronization point at which two threads can exchange data from each other. Call the Exchange () method separately.
Exchanger can be used for genetic algorithms, proofreading work.
Concurrency tool classes in Java