There are several very useful tool classes available in the JDK, namely Countdownlatch,cyclicbarrier and semaphore exchanger
Countdownlatch: Allows one or more threads to wait for another thread to complete the operation
public class countdownlatchtest{
static Countdownlatch C = new Countdownlatch (2);
public static void Main (string[] args) {
New Thread (New Runnable () {
SYSTEM.OUT.PRINTLN (1);
C.countdown ();
SYSTEM.OUT.PRINTLN (2);
C.countdown ();
}). Start ();
C.await ();
System.out.println ("3");
}
}
The Countdownlatch constructor takes an int parameter as a counter, and when you wait for n points to pass N, N is reduced by one when we call Countdownlatch's Countdown method. Countdownlatch's await method blocks until n becomes 0, where n refers to n threads, or n points, and when using multiple threads, only the Countdownlatch can be passed in.
Synchronization barrier Cyclicbarrier
What he's going to do is block a set of threads when they reach a barrier, until the last thread reaches a barrier, the shield opens, and all blocked threads continue to run.
To apply a pseudo-code:
public class cyclicbarriertest{
Static Cyclicbarrier c=new Cyclicbarrier (2);
public static void Main (string[] args) {
New Thread (
New Runnable () {
@overide
public void runnable () {
C.await ();
SYSTEM.OUT.PRINTLN (1);
}
}
). Start ();
C.await ();
System.out.printlN (2);
}
}
Because the CPU allocates time slices of uncertainty, so the output results may also be 2,1
Cycilcbarrier provides a more advanced constructor (int parties, Runnable Runnable)
Runnable says priority is given to runnable after reaching a critical point.
The difference between Cycilcbarrier and Coutdownlatch
COuntdownlatch can only be used once, cycilcbarrier may be used multiple times, can be reset using the Reset method, so cyclicbarrier can be applied to more complex business scenarios. For example, a calculation error can be reset and the result will be recalculated.
Cyclicbarrier method Supplement: Getnumberwaiting Gets the number of blocked threads
IsBroken is interrupted when blocking threads
Control thread concurrency number semaphore (semaphore)
The semaphore semaphore is used to control the number of threads concurrently accessing a particular resource. It coordinates individual threads to ensure reasonable use of public resources
Scenario: can be used as a traffic control, especially for limited scenarios of public resources such as database connections,
Public Class semaphoretest{
private static final int thread_count=30;
private static Threadpoolexecutor ThreadPool = new Threadpoolexecutor (thread_count);
private static Semaphore s = new Semaphore (20);
public static void Main (string[] args) {
Threadpool.executor (New Runnable () {
public void runnable () {
S.acquire ():
System.out.println ("in use ..... ");
S.release ();
}
}):
}
}
Exchanging data between threads exchanger
Exchanger is a tool class for collaboration between threads, Exchanger is used to exchange data between threads, and he provides a switching point at which two threads can exchange data between each other. These two threads Exchange data through the Exchange method, and if the first thread executes the Exchange method, it waits for the second thread to execute the Exchange method, and when two threads execute to the swap point, two threads Exchange data and pass the data produced by this thread to the other party
Application scenario, primarily for verifying the resulting data between two threads
This article is from the "ITER Summary" blog, please be sure to keep this source http://5855156.blog.51cto.com/5845156/1962507
The art of Java concurrency-reading notes-the eighth common concurrency tool class