Semaphore |
Achieve a typical signal volume |
Countdownlatch |
Waits until a specified number of events have occurred |
Cyclicbarrier |
To allow a group of threads to wait at a predefined execution point |
Exchanger |
Exchanging data for two threads |
1. Semaphore
Semaphore (Semaphore), a facility used in multi-threaded environments that coordinates individual threads to ensure that they use public resources correctly and rationally
In Java, you can also set whether the semaphore is in fair mode, and if executed in a fair manner, the thread will be executed in the order of arrival (FIFO), and if it is unfair, then the request may be queued to the head of the queue.
The JDK is defined as follows:
Semaphore (int permits)
Semaphore (intboolean Fair)
// creates a semaphore with the given number of licenses and a given fairness setting.
Semaphore is currently used in multi-threaded environments, the operating system semaphore is a very important concept, in the process of control has been applied.
The Java Concurrency Library semaphore can easily complete semaphore control, and semaphore can control the number of simultaneous accesses to a resource.
get a license through acquire () and wait if not.
void throws Interruptedexception// get a pass void Acquire (intthrows Interruptedexception// get num passes
release () releases a license .
void // Release a pass void release (int// releases multiple passes specified by Num
In order to control access to a resource with a semaphore, each thread that uses the resource must first call the acquire () method before accessing the resource
The release () method must be called when the thread ends its use of the resource.
Semaphore Example:
ImportJava.util.concurrent.Semaphore; Public classSemdemo { Public Static voidMain (string[] args) {Semaphore sem=NewSemaphore (1); NewIncthread ("A", SEM); NewDecthread ("B", SEM); }}//Share static variablesclassshared{Static intCount = 0;}classIncthreadImplementsrunnable{PrivateString name; PrivateSemaphore sem;//Signal Volume Publicincthread (String name,semaphore sem) { This. name=name; This. SEM =sem; NewThread ( This). Start (); } Public voidrun () {System.out.println ("Starting:" +name); Try{System.out.println (name+ "Wait for the semaphore."); Sem.acquire (); SYSTEM.OUT.PRINTLN (Name+ "Get the semaphore."); for(inti = 0; I <5; i++) {Shared.count++;//Modifying static variablesSystem.out.println (name+ ":" +shared.count); Thread.Sleep (10); } }Catch(interruptedexception ext) {ext.printstacktrace (); } System.out.println (Name+ "Release the semaphore."); Sem.release (); } }classDecthreadImplementsrunnable{PrivateString name; PrivateSemaphore sem; Publicdecthread (String name,semaphore sem) { This. name=name; This. SEM =sem; NewThread ( This). Start (); } Public voidrun () {System.out.println ("Starting:" +name); Try{System.out.println (name+ "Wait for the semaphore."); Sem.acquire (); SYSTEM.OUT.PRINTLN (Name+ "Get the semaphore."); for(inti = 0; I <5; i++) {Shared.count--;//Modifying static variablesSystem.out.println (name+ ":" +shared.count); Thread.Sleep (10); } }Catch(interruptedexception ext) {ext.printstacktrace (); } System.out.println (Name+ "Release the semaphore."); Sem.release (); } }
Results:
1234543210B release the semaphore.
Visible, increment 5 times and then decrement 5 times, increment decrement will not mix together.
If the semaphore is not used, access to the Shared.count by two threads may occur synchronously, and the increment and decrement may be mixed together.
2. Countdownlatch
Countdownlatch, a synchronous helper class that allows one or more threads to wait until a set of operations that are performed in another thread is completed.
Main methods
Public Countdownlatch (intpublicvoidpublicvoid Throwspublicvoid await (longthrows
The constructor method parameter specifies the number of times the count must occur before the latch is opened. Digital
Countdown method, the current thread calls this method, then the count is reduced by one
Await method, calling this method will block the current thread until the value of the timer is 0.
The second form waits only within the period specified by wait. The unit of wait is specified by Tu, which is an object of the Timeunit enumeration
Example:
ImportJava.util.concurrent.CountDownLatch; Public classCdldemo { Public Static voidMain (string[] args) {Countdownlatch CD1=NewCountdownlatch (5); System.out.println ("Starting!"); MyThread MT1=NewMyThread (CD1); NewThread (MT1). Start ();//Start Thread Try{cd1.await ();//wait for the latch to open,If this section is commented out, done will appear in the previous}Catch(Interruptedexception exc) {exc.printstacktrace (); } System.out.println ("Done!"); }}classMyThreadImplementsrunnable{Countdownlatch latch; PublicMyThread (Countdownlatch latch) { This. latch=latch; //New Thread (this). Start (); } Public voidrun () { for(inti = 0; I < 5; i + +) {System.out.println ("No." +i); Try{Thread.Sleep (100); } Catch(interruptedexception e) {e.printstacktrace (); } latch.countdown (); } }}
Results:
Await () not annotated Wait until the count is 4 o'clock and then open the latch. Done |
Comment out await () Do not wait for latch to open |
starting! No. 0 1th 2nd 3rd 4th done! |
starting! done! No. 0 1th 2nd 3rd 4th |
3. Cyclicbarrier
Java Learning Note-concurrency tool Semaphore,countdownlatch,cyclicbarrier,exchanger