Common concurrency tool classes in Java

Source: Internet
Author: User

· 1. Countdownlatch waiting for multi-threaded completion

The constructor receives a parameter of type int as a counter and passes in N if you want to wait for n points. When the countdown method of Countdownlatch is called, N is reduced by one until it is reduced to zero. Wait by using the await method, when the value of n becomes 0, the thread performing the await continues execution.

PublicClasscountdowntest {static Countdownlatch C =New Countdownlatch (2);static Executorservice pool = Executors.newfixedthreadpool (2);PublicStaticvoidMainString agrs[]) {Pool.execute (New Runnable () {PublicvoidRun) {try {TimeUnit.MILLISECONDS.sleep (3); }catch (Interruptedexception e) {e.printstacktrace ();} System.out.println ("This is A"); C.countdown ();}}); Pool.execute (new Runnable () { public void Run () { try {TimeUnit.MILLISECONDS.sleep (3);} catch (Interruptedexception e) {e.printstacktrace ();} System. out.println ("This is B"); C.countdown ();}}); try {c.await ();} catch (Interruptedexception e) {} System. out.println ("This is Main");}}              
2. Synchronization Barrier Cyclicbarrier

A barrier that can be used for recycling. What it has to do is block a group of threads from reaching the barrier until the last thread reaches the barrier, and the barrier will open and all the blocked threads will continue to run.

The default constructor method, Cyclicbarrier (int parties), whose parameters represent the number of threads that the barrier intercepts, each thread calls the await method to tell Cyclicbarrier that it has reached the barrier, and then the current thread blocks.

PublicClasscylicbarriertest {static Cyclicbarrier C =New Cyclicbarrier (2);static Executorservice pool = Executors.newfixedthreadpool (2);PublicStaticvoidMainString args[]) {Pool.execute (New Runnable () {PublicvoidRun Out.println ( "This is A"); try {c.await (); System. out.println ( "This is Aa");} catch (Exception e) {e.printstacktrace ();}} }); Pool.execute (new Runnable () { Public void run () {System.out.println ( "This is B"); try {c.await (); System. out.println ( "This is Bb");} catch (Exception e) {e.printstacktrace ();}} }); Pool.shutdown (); }} 
3. Countdownlatch vs Cyclicbarrier

Countdownlatch counters are used only once, while Cyclicbarrier counters can be reset using the Reset method, reused

4. Control the number of thread concurrency semaphore Semaphore

Controls the use of resources for limited-brother threads. Using the method Semaphore SS = new Semaphore (n), n represents the maximum concurrency, each thread uses the resource before calling the Ss.acquire () method to get the permission, and then calls the Ss.release () method to release the permission after use.

5. Data exchange between threads exchanger

It provides a synchronization point at which two threads can exchange data from each other.

PublicClassexchangetest {PrivateStatic final exchanger<string> exgr =New Exchanger<string> ();Privatestatic Executorservice pool = Executors.newfixedthreadpool (2);PublicStaticvoidMainString args[]) {Pool.execute (New Runnable () {Publicvoid Run () {String a = "bank pipelining A"; try {a = Exgr.exchange (a); System. out.println ("A Current value:" + a);} catch (Interruptedexception e) {}}}); Pool.execute (new Runnable () { public void Run () {String b = "bank pipelining B"; try {b = Exgr.exchange (b); System. out.println ("b Current Value:" + b);} catch (Interruptedexception e) {}}}); Pool.shutdown (); }

Common concurrency tool classes in Java

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.