Java Multi-Threading 20: Countdownlatch, Semaphore, Exchanger for other components under multithreading

Source: Internet
Author: User
Tags semaphore

Objective

In a multithreaded environment, the JDK provides developers with a number of components for user use (mainly under java.util.concurrent), so that users do not need to be concerned about how to write code that takes into account both thread safety and efficiency in a specific scenario. Before the thread pool, Blockingqueue are under the Java.util.concurrent component, although the timer is not under the java.util.concurrent, but it is also considered. The latter two articles will briefly explain the use of other components in multiple threads in the form of examples, without the need for a deep understanding of how each component will function.

This article mainly explains is Countdownlatch, Semaphore, Exchanger.

Countdownlatch

The main mechanism provided by Countdownlatch is to trigger an event when more than one thread (the value of the count parameter is equal to the initialization Countdownlatch) has reached the expected state or when the expected work is done, and other threads can wait for the event to trigger their own follow-up work. It is important to note that Countdownlatch is capable of waking up multiple waiting threads.

The thread that reaches its expected state calls the Countdownlatch countdown method, and the waiting thread calls the Countdownlatch await method. If the count value of Countdownlatch initialization is 1, then this is degraded to a single event, that is, a thread notifies other threads, and the effect is equal to the object's wait and Notifyall,count values greater than 1 are common ways The goal is to have multiple threads reach their expected state, become an event notification, and the thread continue its behavior.

See an example:

PrivateStaticClass WorkthreadExtendsthread{PrivateCountdownlatch CDL;PrivateIntSleepsecond;Public Workthread (String name, Countdownlatch CDL,IntSleepsecond) {Super(name);THIS.CDL =CdlThis.sleepsecond =Sleepsecond; }PublicvoidRun () {Try{System.out.println (This.getname () + "started, Time is" +System.currenttimemillis ()); Thread.Sleep (Sleepsecond * 1000); Cdl.countdown (); System.out.println (This.getname () + "done, time is" +System.currenttimemillis ()); }Catch(Interruptedexception e) {E.printstacktrace ();} }}PrivateStaticClass DonethreadExtendsthread{PrivateCountdownlatch CDL;PublicDonethread (String name, Countdownlatch CDL) {Super(name);THIS.CDL =Cdl }PublicvoidRun () {Try{System.out.println (This.getname () + "to wait, time for" +System.currenttimemillis ()); Cdl.await (); System.out.println (This.getname () + "Wait is over, time is" +System.currenttimemillis ()); }Catch(Interruptedexception e) {E.printstacktrace ();} }}PublicStaticvoid main (string[] args) throws  exception{Countdownlatch CDL = new Countdownlatch (3 Span style= "color: #000000;" >); Donethread dt0 = new donethread ("DoneThread1" new donethread ("DoneThread2" new workthread ("WorkThread1", CDL, 2new workthread ("WorkThread2", CDL, 3new workthread ("WorkThread3", CDL, 4              

Look at the results of the operation:

DoneThread2 to wait, time for 1444563077434donethread1 to wait, time for 1444563077434workthread1 started, time for 1444563077434workthread3 started, Time for 1444563077435workthread2 started, time for 1444563077435workthread1 execution, time for 1444563079435workthread2 executed, Time for 1444563080435workthread3 execution finished, time for 1444563081435donethread1 wait, time for 1444563081435donethread2 wait is finished, Time is 1444563081435

The effect is very obvious, explain:

1. Start 2 threads donethread thread wait 3 workthread all finished

2, 3 Workthread all finished, the last execution of the WorkThread3 executed a second in line with the expected

3, the last three sentences from time to see almost at the same time, indicating that Countdownlatch set to 3,workthread3 execution, two wait for the thread immediately executes the following code

This is equivalent to an evolutionary version of the wait/notification mechanism, which can be implemented after multiple worker threads have completed a task to notify multiple waiting threads to start working, before a worker thread finishes the task to notify a waiting thread or a worker thread to complete the task to notify all waiting threads.

Countdownlatch is actually useful, especially for this scenario where a problem is divided into n parts, and after all the sub-parts are complete, notify another/several threads to start working. For example, I want to statistics C, D, E, f files, you can open 4 threads, respectively, statistics C, D, E, F disk files, statistical completion of the file information summarized into another/several threads for processing

Semaphore

Semaphore is a very useful component that is equivalent to a concurrent controller and is used to manage semaphores. When constructing a value that is passed to the managed semaphore, this value is the control of the concurrency, we need to control the concurrency code, before execution through the acquire method to obtain the signal, after execution through release to return the signal. Each time the acquire returns successfully, the semaphore available semaphore will be reduced by one, and if no signal is available, the acquire call will block and wait for the release call to free the signal before acquire will get the signal and return.

The semaphore is divided into single-valued and multi-valued two types:

1, the single-valued semaphore management of only 1 semaphores, the semaphore can only be 1, can only be obtained by a thread, meaning that the concurrent code can only be run by one thread, which is equivalent to a mutual exclusion lock

2, multi-value semaphore management of the signal amount of 1, mainly used to control the number of concurrent

Take a look at the code example:

PublicStaticvoidMain (string[] args) {Final Semaphore Semaphore =New Semaphore (5); Runnable Runnable =NewRunnable () {PublicvoidRun () {Try{Semaphore.acquire ();
System.out.println (Thread.CurrentThread (). GetName () + "received semaphore, time" +System.currenttimemillis ()); Thread.Sleep (2000); System.out.println (Thread.CurrentThread (). GetName () + "releases the semaphore, Time is" +catch (Interruptedexception e) {e.printstacktrace ();} finally {semaphore.release ();}}}; thread[] Threads = "; for (int i = 0; i < threads.length; I++) threads[i] = new Thread (runnable); for (int i = 0; i < threads.length; I++) Threads[i].start ();

Look at the results of the operation:

1 thread-1 received the semaphore, the time is 14445570404642 thread-2 received the semaphore, the time is 14445570404653 thread-0 received the semaphore, the time is 14445570404644 thread-3 received the semaphore, the time is 14445570404655 thread-4 received the semaphore, the time is 14445570404656 thread-2 The semaphore is released and the time is 14445570424667 thread-4 The semaphore is released and the time is 14445570424668 thread-0 The semaphore is released and the time is 14445570424669 thread-1 The semaphore is released and the time is 1444557042466Ten thread-3 The semaphore is released and the time is 1444557042466thread-9 received a semaphore, time 144455704246712 thread-7 obtained the semaphore, time is 144455704246613 thread-6 got the semaphore, The time is 144455704246614 thread-5 received a semaphore, time of 1444557042466 Span style= "color: #008080;" >15 thread-8 received a semaphore, time 144455704246716 Thread-9 released the semaphore at a time of 144455704446717 thread-6 released the semaphore, Time is 144455704446718 thread-7 released the semaphore, Time 1444557044467 Span style= "color: #008080;" >19 thread-5 released the semaphore, Time 144455704446820 Thread-8 released the semaphore , Time is 1444557044468               

The first 10 behavior part, the thread that runs is 1 2 0 3 4, see the time difference also is the code contract 2 second, the post 10 behavior part, the thread that runs is 9 7 6 5 8, Time difference also is the contract 2 second, this manifests the function of semaphore.

This method of controlling concurrent concurrency by Semaphore is less granular than controlling the number of threads to control the number of concurrent numbers, because semaphore can control the concurrency of code blocks by means of acquire and release methods.

Last note two points:

1, Semaphore can specify a fair lock or a non-fair lock

2, acquire method and release method can have parameters, indicating the number of acquisition/return of the semaphore

Exchanger

Exchanger, in the name of the understanding is the exchange. The exchanger is used to exchange data between two threads, noting that data exchange can only be done between two threads. The thread blocks on the exchanger exchange method until another thread has reached the same exchanger exchange method, exchanging data, and then two threads continue to execute their own related code.

Exchanger has only one exchange method for exchanging data. Take a look at the example:

PublicStaticClass ExchangerthreadExtendsthread{PrivateString str;Private exchanger<string>ExchangerPrivateIntSleepsecond;Public Exchangerthread (String str, exchanger<string> Exchanger,IntSleepsecond) {This.str =StrThis.exchanger =ExchangerThis.sleepsecond =Sleepsecond; }PublicvoidRun () {Try{System.out.println (This.getname () + "Start, the original data is" + str + ", the time is" +System.currenttimemillis ()); Thread.Sleep (Sleepsecond * 1000); str =Exchanger.exchange (str); System.out.println (this.getname () + "exchange data, the data exchanged is" + str + ", the time is" + System.currenttimemillis ());} Catch (Interruptedexception e) {e.printstacktrace ();}}} public static void main (string[] args) {exchanger<string> Exchanger = new exchanger<string >(); Exchangerthread et0 = new Exchangerthread ("111", exchanger, 3); Exchangerthread Et1 = new Exchangerthread ("222", Exchanger, 2); Et0.start (); Et1.start ();}   

Look at the results of the operation:

thread-0 Start, the original data is 111, the time is 1444560972303thread-1 start, the original data is 222, the time is 1444560972303thread-0 Exchange data, the data after Exchange is 222, The time is 1444560975303thread-1 Exchange data, the Exchange data is 111, time is 1444560975303

See two threads exchanged data, because a thread sleeps 2 seconds, a thread sleeps 3 seconds, since to exchange data, must sleep 2 seconds to wait for 3 seconds to sleep, so see the time difference is 3000ms is 3s.

From this example, exchanger is a bit like the two-way form of Java multithreading 15:queue, Blockingqueue, and Synchronousqueue using Blockingqueue to implement the producer/consumer model article. It may be useful in genetic algorithms and piping design.

Java Multi-Threading 20: Countdownlatch, Semaphore, Exchanger for other components under multithreading

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.