The semaphore (Semaphore), sometimes called a semaphore, is a facility used in a multithreaded environment that coordinates individual threads to ensure that they are able to use public resources correctly and rationally.
A count semaphore. Conceptually, semaphores maintain a set of licenses. If necessary, block each acquire () before the license is available, and then obtain the license. Each release () adds a license that may release a blocked fetch. However, instead of using the actual license object, Semaphore only counts the number of available licenses and takes action accordingly. The thread that gets the semaphore can enter the code or wait. Obtain and release access licenses through Acquire () and release ().
Package Com.lala.shop;import Java.time.duration;import Java.time.instant;import java.util.arrays;import Java.util.collections;import Java.util.list;import Java.util.concurrent.countdownlatch;import Java.util.concurrent.executorservice;import Java.util.concurrent.executors;import Java.util.concurrent.Semaphore ; Import Java.util.concurrent.timeunit;public class Semaphoredemo {/** * Here's an example: 10 people going to the bathroom together * if you can only go to the bathroom at a time, it will take you a total of one day: 1+2+ 3+4+5+6+7+8+9+10=55 min * If you can go to the toilet with 10 people at a time: 10 minutes */static void demonstration (int num) throws exception{string[] Users = {"Liu Mei", "nod", "Xia Xue", "Liu Xing", "Summer Rain", "Grandpa", "Granny", "Mary", "Hu Unified", "an Lam Ninh"}; Countdownlatch CDL = new Countdownlatch (users.length); integer[] times = {1,2,3,4,5,6,7,8,9,10}; List<integer> timelist = Arrays.aslist (times); Collections.shuffle (Timelist); Semaphore SPH = new Semaphore (num); Executorservice runner = Executors.newcachedthreadpool (); Instant start = Instant.now (); for (int i=0; i<users.length; i++) {Final int index = I;runner.submit (), {try {//get beacon, prepareToilet Sph.acquire (); Long time = Timelist.get (index); TimeUnit.SECONDS.sleep (time); System.out.println (Users[index] + "toilet complete, Time spent:" + "; Cdl.countdown ();//The thing has already been done, release the Beacon Sph.release ();} catch (Exception e) {e.printstacktrace ();}});} Runner.shutdown (); cdl.await () Instant end = Instant.now ();D uration speed = Duration.between (start, end); Long seconds = Speed.getseconds ();///Seconds means System.out.println ("all the toilets (only" + num + "People go to the toilet at a time), taking a total of hours:" + seconds);} public static void Main (string[] args) throws exception{demonstration (5);}}
If the method is called:
Demonstration (5);
Then, the output is:
Summer snow toilet completed, spent time: 1
Summer Rain toilet finished, spent time: 3
Liu Xing toilet completed, spent time: 4
Grandpa toilet Complete, spent time: 6
Nod toilet completed, spent time: 8
Liu Mei toilet completed, spent time: 9
Hu Unified toilet Complete, spent time: 2
Marie toilet completed, spent time: 7
An Lam Ninh toilet completed, spent time: 5
Granny toilet complete, spend time: 10
All over the toilet (only 5 people go to the toilet at a time), it took a total of: 13
If the method is called:
Demonstration (1);
Then, the output is:
Liu Mei toilet completed, spent time: 10
Nod toilet completed, spent time: 6
Summer snow toilet completed, spent time: 3
Liu Xing toilet completed, spent time: 9
Summer Rain toilet finished, spent time: 8
Grandpa toilet Complete, spent time: 4
Granny toilet complete, spend time: 1
Marie toilet completed, spent time: 5
Hu Unified toilet Complete, spent time: 7
An Lam Ninh toilet completed, spent time: 2
All over the toilet (only 1 people go to the toilet at a time), it took a total of: 55
If the call method is
Demonstration (10); The output is:
Summer Rain toilet finished, spent time: 1
Nod toilet completed, spent time: 2
Grandpa toilet Complete, spent time: 3
Liu Xing toilet completed, spent time: 4
Liu Mei toilet completed, spent time: 5
Hu Unified toilet Complete, spent time: 6
An Lam Ninh toilet completed, spent time: 7
Granny toilet complete, spend time: 8
Marie toilet completed, spent time: 9
Summer snow toilet completed, spent time: 10
All over the toilet (only 10 people go to the toilet at a time), it took a total of: 10
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
The semaphore of Java concurrent programming