[Java concurrent programming] 23: concurrency new feature-Semaphore (including code)

Source: Internet
Author: User

 

In the operating system, semaphores are a very important concept and play an important role in controlling the collaboration between processes. Through different operations on semaphores, mutual Exclusion and synchronization between processes can be achieved separately. Of course, it can also be used for multi-thread control. We can use semaphores to customize synchronized, wait, and y mechanisms similar to those in Java.

The semaphores Semaphore in the Java concurrent package is actually a counting semaphores with completed functions. In concept, it maintains a license set, it is of great significance to control the consumption and recovery of certain resources. Semaphore can control the number of tasks simultaneously accessed by a resource. It obtains a license through acquire (), and release () releases a license. If the number of simultaneously accessed tasks is full, the other acquire tasks enter the waiting state until one task is release.

The following is an example program that uses Semaphore to control the number of concurrent accesses:

 

Import java. util. concurrent. executorService; import java. util. concurrent. executors; import java. util. concurrent. semaphore; public class SemaphoreTest {public static void main (String [] args) {// use the new features to start and manage threads-internally use the thread pool ExecutorService exec = Executors. newCachedThreadPool (); // only five threads can simultaneously access final Semaphore semp = new Semaphore (5); // simulate 10 clients to access for (int index = 0; index <10; index ++) {final int num = index; Runnable run = new Runnable () {public void run () {try {// obtain the license semp. acquire (); System. out. println (Thread + Thread. currentThread (). getName () + license: + num); // simulate time-consuming tasks for (int I = 0; I <999999; I ++); // release the license semp. release (); System. out. println (Thread + Thread. currentThread (). getName () + release license: + num); System. out. println (number of tasks currently allowed to enter: + semp. availablePermits ();} catch (InterruptedException e) {e. printStackTrace () ;}}; exec.exe cute (run) ;}// close the thread pool exec. shutdown ();}}
The result of an execution is as follows:

 

Thread pool-1-thread-1 license: 0
Thread pool-1-thread-1 release license: 0
Number of tasks currently allowed: 5
Thread pool-1-thread-2 licensed: 1
Thread pool-1-thread-6 license: 5
Thread pool-1-thread-4 License: 3
Thread pool-1-thread-8 licensed: 7
Thread pool-1-thread-2 release license: 1
Number of tasks currently allowed: 2
Thread pool-1-thread-5 licensed: 4
Thread pool-1-thread-8 release license: 7
Thread pool-1-thread-3 licensed: 2
Thread pool-1-thread-4 release license: 3
Thread pool-1-thread-10 license: 9
Thread pool-1-thread-6 release license: 5
Thread pool-1-thread-10 release license: 9
Number of tasks currently allowed: 2
Thread pool-1-thread-3 release license: 2
Number of tasks currently allowed: 1
Thread pool-1-thread-5 release license: 4
Number of tasks currently allowed: 3
Thread pool-1-thread-7 License: 6
Thread pool-1-thread-9 licensed: 8
Thread pool-1-thread-7 release license: 6
Number of tasks currently allowed: 5
Number of tasks currently allowed: 3
Number of tasks currently allowed: 3
Number of tasks currently allowed: 3
Thread pool-1-thread-9 Release license: 8
Number of tasks currently allowed: 5

It can be seen that the number of tasks allowed for concurrent access by Semaphore is always 5. Of course, it is easy to see that Semaphore only monitors the number of concurrent access tasks of resources, thread security is not guaranteed. Therefore, you must control the secure access of the thread on your own during access.

 

 

 

 

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.