JDK5 new features thread Synchronization tool Class (III)

Source: Internet
Author: User
Tags semaphore

I. Semaphore realization OF signal lights

Semaphore can control the number of threads that concurrently access resources, such as implementing the number of concurrent accesses allowed by a file.

semaphore implementation of the function is similar to the toilet has 5 pits, joined 10 people to go to the toilet, so at the same time only 5 people can occupy, when any of the 5 persons left, one of the other 5 people waiting for it would be occupied. the other 5 people who are waiting can be given a chance at random, or they can get an opportunity in the order of first served, depending on the parameter options passed in when constructing the semaphore object.

/** * Semaphore: Semaphore */public class Semaphoretest {public static void main (string[] args) {//Create a thread pool executorservice es = E Xecutors.newcachedthreadpool (); The number of threads that allow concurrent access is 3 final Semaphore sp = new Semaphore (3),//Open 10 threads for (int i = 0; i <; i++) {Es.execute (New Runnable ( {@Overridepublic void Run () {try {sp.acquire ();//Get Semaphore System.out.println ("thread" + thread.currentthread (). GetName () + " Enter, there is currently "+ (3-sp.availablepermits ()) +" a concurrency! "); Thread.Sleep (2000); SYSTEM.OUT.PRINTLN ("Thread" + thread.currentthread (). GetName () + "leaving soon!" "); Sp.release (); Release the semaphore System.out.println ("thread" + thread.currentthread (). GetName () + "left, currently has" + (3-sp.availablepermits ()) + "Concurrency! ");} catch (Interruptedexception e) {e.printstacktrace ();}}});}}}

A single Semaphore object can implement the function of a mutex, and it can be a thread that obtains a "lock", and another thread releases the "lock", which can be applied to some occasions of deadlock recovery.


public class Cyclicbarriertest {public static void main (string[] args) {Executorservice service = Executors.newcachedthre Adpool (); final Cyclicbarrier cb = new Cyclicbarrier (3);//create 3 threads for (int i=0;i<3;i++) {Service.execute (New Runnable () {@Overridepublic void Run () {try {Thread.Sleep ((long) (Math.random () * 10000));//thread "Rest" time is different System.out.println ("thread" + Thread.CurrentThread (). GetName () + "Approaching meeting point 1, currently available" + (cb.getnumberwaiting () + 1) + "One has arrived," + (cb.getnumberwaiting () = = 2?                       "All is up, move on": "Waiting"); Cb.await (); First-come wait-to-arrive, and when 3 arrives, it will continue to execute Thread.Sleep ((Long) (Math.random () * 10000));//thread "Rest" time is different System.out.println ("thread" + Thr Ead.currentthread (). GetName () + "Approaching meeting point 2, currently available" + (cb.getnumberwaiting () + 1) + "One has arrived," + (cb.getnumberwaiting () = = 2? "All is up, move on": "Waiting"); cb.await (); Thread.Sleep ((Long) (Math.random () *10000));//Each thread "breaks" the time differently System.out.println ("thread" + thread.currentthread (). GetName () + "arriving at meeting point 3, currently available" + (cb.getnumberwaiting () + 1) + "One has arrived,"+ (cb.getnumberwaiting () = = 2?                     "All is up, move on": "Waiting");                     Cb.await ();               } catch (Exception e) {e.printstacktrace ();} }});} Service.shutdown ();}}


JDK5 new features thread Synchronization tool Class (III)

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.