1. Semaphore concept
Semaphore is a synchronization tool provided after Java1.5, semaphore can maintain access to its own number of threads and provides a synchronization mechanism. Using semaphore, you can control the number of threads that access resources concurrently, obtain a license through acquire (), and release () releases a license if there is no wait.
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 another 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 the function of mutex, and can be obtained by one thread "lock", and then by another thread release "lock", which can be applied to some occasions of deadlock recovery. Therefore, the function of the semaphore object of a single semaphore is common with the synchronized realization of mutual exclusion.
2, the function expands 1, the semaphore often unifies the thread pool to use, for example establishes a fixed size 10 thread pool, the maximum number of threads concurrency is 10, when you commit 20 tasks to the thread pool, the thread pool arranges these 10 threads to receive 20 tasks first 10, When 10 of the priorities are completed, they will receive one of the remaining 10 tasks until all tasks have been performed. However, if the semaphore object is introduced at this time, and the value is 5, then only 5 of the 10 threads in this thread pool can execute concurrently, and this is done with limited access. 2, when the semaphore constructor method is passed in the parameter is 1, at this time the number of threads concurrency is 1, that is, thread-safe, this way can also be done on-site mutual exclusion. Java implements mutex thread synchronization there are three ways synchronized, lock, single semaphore
3, the use of semaphore demo is as follows
- public class Semahoredemo {
- Public static void main(string[] args) {
- Executorservice Executorservice = Executors.newcachedthreadpool ();
- final Semaphore Semaphore = new Semaphore (1,true);
- For (int i=0;i<10;i++) {
- Runnable Runnable = new Runnable () {
- @Override
- Public void Run() {
- try {
- Semaphore.acquire ();
- } catch (Interruptedexception e) {
- E.printstacktrace ();
- }
- System.err.println ("thread" +thread.currentthread (). GetName () +"Enter, already" + (3-semaphore.availablepermits ()) + "concurrency");
- try {
- Thread.Sleep ((Long) (Math.random () *));
- } catch (Interruptedexception e) {
- E.printstacktrace ();
- }
- System.out.println ("thread" +thread.currentthread (). GetName () +"about to leave");
- Semaphore.release ();
- System.err.println ("thread" +thread.currentthread (). GetName () +"already left" +"current Concurrency:" + (3- Semaphore.availablepermits ()));
- }
- };
- Executorservice.execute (runnable);
- }
- Executorservice.shutdown ();
- }
- }
Java Advanced Beacon Semaphore