Introduction to Java semaphores Semaphore

Source: Internet
Author: User

Semaphore is being expanded and used in multi-threaded environments. The Semaphore of the operating system is an important concept and has been applied in Process Control. The Semaphore of the Java concurrent library can easily implement Semaphore control. Semaphore can control the number of concurrent accesses to a resource and obtain a license through acquire (). If not, wait, while release () releases a license. For example, in Windows, you can set the maximum number of client accesses to shared files.

The functions implemented by Semaphore are similar to five pitfall in the restroom. If 10 people need to go to the restroom, how many people can go to the restroom at the same time? At the same time, only five people can occupy the resources. when any one of the five people gives up, another of the other five waiting for the resources can occupy the resources. In addition, among the five waiting persons, the opportunity can be obtained at random or in the order of first come and then, depending on the parameter options passed in when the Semaphore object is constructed. The Semaphore object of a single Semaphore can implement the mutex lock function, and the lock can be obtained by one thread, and then released by another thread ", this can be applied to deadlock recovery.

Semaphore maintains the current number of accesses and provides a synchronization mechanism to control the number of simultaneous accesses. In the data structure, the linked list can store "unlimited" nodes and use Semaphore to implement a limited-size linked list. In addition, ReentrantLock can also be implemented, but the implementation is more complicated.
In the Demo below, a Semaphore with only five licenses is declared, and 20 threads are required to access this resource, and access permissions are obtained and released through acquire () and release.
Package com. test;
Import java. util. concurrent. ExecutorService;
Import java. util. concurrent. Executors;
Import java. util. concurrent. Semaphore;
Public class TestSemaphore {
Public static void main (String [] args ){
// Thread Pool
ExecutorService exec = Executors. newCachedThreadPool ();
// Only five threads can simultaneously access
Final Semaphore semp = new Semaphore (5 );
// Simulate access from 20 clients
For (int index = 0; index <20; index ++ ){
Final int NO = index;
Runnable run = new Runnable (){
Public void run (){
Try {
// Obtain the license
Semp. acquire ();
System. out. println ("Accessing:" + NO );
Thread. sleep (long) (Math. random () * 10000 ));
// Release after access
Semp. release ();
System. out. println ("-----------------" + semp. availablePermits ());
} Catch (InterruptedException e ){
E. printStackTrace ();
}
}
};
Exec.exe cute (run );
}
// Exit the thread pool
Exec. shutdown ();
}
}
The execution result is as follows: www.2cto.com
Accessing: 0
Accessing: 1
Accessing: 3
Accessing: 4
Accessing: 2
----------------- 0
Accessing: 6
----------------- 1
Accessing: 7
----------------- 1
Accessing: 8
----------------- 1
Accessing: 10
----------------- 1
Accessing: 9
----------------- 1
Accessing: 5
----------------- 1
Accessing: 12
----------------- 1
Accessing: 11
----------------- 1
Accessing: 13
----------------- 1
Accessing: 14
----------------- 1
Accessing: 15
----------------- 1
Accessing: 16
----------------- 1
Accessing: 17
----------------- 1
Accessing: 18
----------------- 1
Accessing: 19

Author: guojunjian

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.