Semaphore is a very useful component that is equivalent to a concurrent controller and is used to manage semaphores. when constructed, the value of the semaphore that is managed is passed in, which controls the number of concurrent quantities , which can be accessed by several threads at the same time. We need to control the concurrency of the Code, before execution through the acquire method to obtain the signal, after execution through release to return the signal. Each time the acquire returns successfully, the semaphore available semaphore will be reduced by one, and if no signal is available, the acquire call will block and wait for the release call to free the signal before acquire will get the signal and return.
PS: note here that the semaphore acquire method and the release method can have parameters, indicating the number of semaphores obtained/returned, if not specified, is the default single release
semaphore implementation of the function is similar to the toilet has 5 pits, if there are 10 people to the toilet, then at the same time only how many people to go 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 be given an opportunity in the order of first served, depending on the parameter options passed in when constructing the semaphore object. The semaphore object of a single semaphore can implement a mutex function, 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. semaphore is excellent at implementing this queueing mechanism, and the code is simple
Semaphore maintains the number of current accesses, provides a synchronization mechanism, and controls the number of simultaneous accesses. In the data structure, the linked list can save the "infinite" node, and the semaphore can be used to realize the finite-size linked list. In addition, the re-entry lock Reentrantlock can also implement this function, but the implementation is more complicated.
Reentrantlock Implementation of the production/consumer one-on-one case, contrastSemaphore Code is more complex, but in multiple production/consumption situations,Reentrantlock Although the code is more complex, but more efficient
The semaphore is divided into single-valued and multi-valued two types:
1, the single-valued semaphore management of only 1 semaphores, the semaphore can only be 1, can only be obtained by a thread, meaning that the concurrent code can only be run by one thread, which is equivalent to a mutual exclusion lock
2, multi-value semaphore management of the signal amount of 1, mainly used to control the number of concurrent
Take a look at the code example:
Public Static voidMain (string[] args) {FinalSemaphore Semaphore =NewSemaphore (5); Runnable Runnable=NewRunnable () { Public voidrun () {Try{semaphore.acquire ();
System.out.println (Thread.CurrentThread (). GetName ()+ "Get the semaphore, time is" +System.currenttimemillis ()); Thread.Sleep (2000); System.out.println (Thread.CurrentThread (). GetName ()+ "Release the semaphore, Time is" +System.currenttimemillis ()); } Catch(interruptedexception e) {e.printstacktrace (); } finally{semaphore.release (); } } }; thread[] Threads=NewThread[10]; for(inti = 0; i < threads.length; i++) Threads[i]=NewThread (runnable); for(inti = 0; i < threads.length; i++) Threads[i].start ();}
Look at the results of the operation:
1thread-1 received the semaphore, the time is 14445570404642thread-2 received the semaphore, the time is 14445570404653thread-0 received the semaphore, the time is 14445570404644thread-3 received the semaphore, the time is 14445570404655thread-4 received the semaphore, the time is 14445570404656thread-2 Release The semaphore, Time is 14445570424667thread-4 Release The semaphore, Time is 14445570424668thread-0 Release The semaphore, Time is 14445570424669thread-1 Release The semaphore, Time is 1444557042466Tenthread-3 Release The semaphore, Time is 1444557042466 Onethread-9 received the semaphore, the time is 1444557042467 Athread-7 received the semaphore, the time is 1444557042466 -thread-6 received the semaphore, the time is 1444557042466 -thread-5 received the semaphore, the time is 1444557042466 thethread-8 received the semaphore, the time is 1444557042467 -thread-9 Release The semaphore, Time is 1444557044467 -thread-6 Release The semaphore, Time is 1444557044467 -thread-7 Release The semaphore, Time is 1444557044467 +thread-5 Release The semaphore, Time is 1444557044468 -Thread-8 released the semaphore, Time is 1444557044468.
The first 10 behavior part, the thread that runs is 1 2 0 3 4, see the time difference also is the code contract 2 seconds, the post 10 behavior part, the running thread is 9 7 6 5 8, the time difference also is the contract 2 seconds, this manifests the semaphore function, here because is in the middle uses sleep, So it seems to be orderly, must release 5 to obtain, in fact, is a release, Semaphore discovery and spare will immediately be assigned to the next waiting thread .
This method of controlling concurrent concurrency by Semaphore is less granular than controlling the number of threads to control the number of concurrent numbers, because semaphore can control the concurrency of code blocks by means of acquire and release methods.
Last note two points:
1, Semaphore can specify a fair lock or a non-fair lock
2, acquire method and release method can have parameters, indicating the number of acquisition/return of the semaphore
Java Multi-Threading 28: Semaphore Semaphore for multithreaded components