Semaphore is in the current multi-threaded environment is used, single-threaded can not be used, remember that this is the premise of semaphore use, must be multithreaded environment, semaphore to maintain the current access to their own number of threads, and provide a synchronization mechanism, Use semaphore to control the number of threads concurrently accessing resources, for example
Toilets have 5 pits, if there are 10 people to go to the toilet, then at the same time only how many people to the toilet? At the same time only 5 people can occupy, when any one of the 5 people out of the way, one of the other 5 waiting for a person to occupy. 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, now write a test class to use
Semaphoretest.java
public class Semaphoretest {public static void main (string[] args) {Executorservice service = Executors.newcachedthreadpo OL (); final Semaphore sp = new Semaphore (3); for (int. i=0;i<10;i++) {Runnable Runnable = new Runnable () {public void Ru N () {try {sp.acquire ();} catch (Interruptedexception E1) {e1.printstacktrace ();} SYSTEM.OUT.PRINTLN ("Thread" + thread.currentthread (). GetName () + "Enter, current already" + (3-sp.availablepermits ()) + "concurrency"); try { Thread.Sleep ((Long) (Math.random () *10000));} catch (Interruptedexception e) {e.printstacktrace ();} SYSTEM.OUT.PRINTLN ("Threads" + thread.currentthread (). GetName () + "going Away"); Sp.release ();//The following code sometimes does not perform accurately, Because it does not synthesize atomic units with the above code System.out.println ("thread" + thread.currentthread (). GetName () + "left, current already" + (3-sp.availablepermits ()) + "concurrency");}; Service.execute (runnable);}}}
Printing results:
/p>
Thread Pool-1-thread-1 entered, there are currently 1 concurrent threads Pool-1-thread-4 entered, there are currently 2 concurrent threads Pool-1-thread-8 entered, There are currently 3 concurrent threads pool-1-thread-4 leaving the thread pool-1-thread-4 has left, and there are currently 2 concurrent threads pool-1-thread-2 into the There are currently 3 concurrent threads pool-1-thread-8 leaving the thread pool-1-thread-8 has left, and there are currently 2 concurrent threads pool-1-thread-6 into the There are currently 3 concurrent threads pool-1-thread-1 leaving the thread pool-1-thread-1 has left, and there are currently 2 concurrent threads pool-1-thread-10 into the There are currently 3 concurrent threads pool-1-thread-10 leaving the thread pool-1-thread-10 has left, and there are currently 2 concurrent threads pool-1-thread-5 into the There are currently 3 concurrent threads pool-1-thread-2 leaving the thread pool-1-thread-2 has left, and there are currently 2 concurrent threads pool-1-thread-9 into the There are currently 3 concurrent threads pool-1-thread-6 leaving the thread pool-1-thread-6 has left, and there are currently 2 concurrent threads pool-1-thread-7 into the There are currently 3 concurrent threads pool-1-thread-5 leaving the thread pool-1-thread-5 has left, and there are currently 2 concurrent threads pool-1-thread-3 into the There are currently 3 concurrent threads pool-1-thread-7 leaving the thread pool-1-thread-7 has left, there are currently 2 concurrent threads pool-1-thread-9 is about to leave the thread pool-1-thread-9 has left, There are currently 1 concurrent threads pool-1-thread-3 leaving the thread pool-1-thread-3 has left, and there are currently 0 concurrent
Semaphore in Java