Semaphore maintaining the number of threads currently accessing itself

Source: Internet
Author: User

The number of users who can achieve traffic control while accessing files

import java.util.concurrent.executorservice;import java.util.concurrent.executors;import  java.util.concurrent.semaphore;public class semaphoretest {    public  Static void main (String[] args)  {         Executorservice service = executors.newcachedthreadpool ();         final  semaphore sp = new semaphore (3);//Create Semaphore semaphore, initialize license size 3         runnable runnable = new runnable () {             public void run () {             try {                 sp.acquire ()///request permission to continue execution if a license is available, minus 1 of the license. Otherwise enter the blocking state             } catch  (INTERRUPTEDEXCEPTION&NBSP;E1)  {                 e1.printstacktrace ();             }        &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;SYSTEM.OUT.PRINTLN ("Thread"  + thread.currentthread (). GetName ()  +                        "Enter, currently available"  +  (3-sp.availablepermits ())  +  "concurrency");             try {                 thread.sleep ((Long) (Math.random () *10000));             } catch  (interruptedexception e)  {      &nBsp;          e.printstacktrace ();             }           &NBSP;&NBSP;SYSTEM.OUT.PRINTLN ("Thread"  + thread.currentthread (). GetName ()  +                       " Going away ");                                 sp.release ();//release permission, Number of licenses plus 1            //The following code is sometimes executed inaccurately because it does not synthesize atomic units with the above code &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;SYSTEM.OUT.PRINTLN ("Threads"  +  Thread.CurrentThread (). GetName ()  +                       "left, currently available"  +  (3-sp.availablepermits ())  +  "concurrency");                              }    };         for (int i=0;i<10;i++) {             service.execute (runnable);  //Submit 10 Quests                     }    }                                                                                                                         }


This article is from the "thick, thin hair, ax" blog, please be sure to keep this source http://tianxingzhe.blog.51cto.com/3390077/1716824

Semaphore maintaining the number of threads currently accessing itself

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.