Seconds to kill multithreading eighth classic thread sync semaphore semaphore (cont.)

Source: Internet
Author: User

Java Semaphore Implementation:

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 semaphore of the Java Concurrency Library makes it easy to control the semaphore, Semaphore can control the number of simultaneous accesses to a resource, obtain a license through acquire (), and release () releases a license if there is no waiting. For example, under Windows, you can set the maximum number of client accesses for a shared file.

 PackageCom.multithread.semaphore;ImportJava.util.concurrent.Semaphore;ImportCom.multithread.main.ExampleInterface;/*** Semaphore can control the number of simultaneous use of a resource. * Get permission through acquire (), no wait. * Release () releases a license. *  * */ Public classSemaphoreexampleextendsexampleinterface{FinalSemaphore SEMA =NewSemaphore (1); intG_num = 0; @Override Public voidStartdemo () { for(inti=0;i<10;i++) {Thread se=NewSemamultithread (I,sema);        Se.start (); }} @Override Public voidStartDemo2 () {FinalSemaphore _lsema =NewSemaphore (10);  for(inti=0;i<50;i++) {Thread se=NewSemamultithread (I,_lsema);        Se.start (); }    }    classSemamultithreadextendsThread {intindex =-1; Semaphore Msema=NULL;  PublicSemamultithread (intI,semaphore SE) {             This. index =i; Msema=se; } @Override Public voidrun () {Try{msema.acquire (); System.out.println ("Index:" + This. Index); Thread.Sleep ((Long) (Math.random () *100));                Msema.release (); System.out.println ("----------------:"+msema.availablepermits ()); Thread.Sleep ((Long) (Math.random () *100)); } Catch(interruptedexception e) {//TODO auto-generated Catch blockE.printstacktrace (); }        }    }}

You can make each thread mutually exclusive: final Semaphore sema = new Semaphore (1);

New Semaphore (ten);  At the same time, the maximum of 10 threads is running, similar to the effect of a thread pool!

Seconds to kill multithreading eighth classic thread sync semaphore semaphore (cont.)

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.