Java Semaphore semaphore, java signal Semaphore

Source: Internet
Author: User

Java Semaphore semaphore, java signal Semaphore
Semaphore

Semaphore is divided into two types: single value and multi-value. The former can only be obtained by one thread, and the latter can be obtained by several threads.

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.

Take a parking lot as an example. For the sake of simplicity, assuming that there are only three parking spaces in the parking lot, all three parking spaces are empty at the beginning.At this time, if there are five vehicles at the same time, the gatekeeper will allow three of them to enter without hindrance, and then put down the car, and the remaining cars will have to wait at the entrance, since then, all the cars have to wait at the entrance.. At this time,A car leaves the parking lot. When the gatekeeper learns, he opens the car and places it in a car.If two more vehicles are left, they can be placed in two more vehicles.

In this parking lot system, a parking space is a public resource. Every car is like a thread. The Gatekeeper plays the role of a semaphore.

Furthermore, semaphores have the following features: semaphores are non-negative integers.(Number of parking spaces ),All threads passing through it(Vehicle)This integer is reduced by one.(Use resources ),When the integer is zero, all threads that attempt to pass it will be in the waiting state. In semaphores, we define two types of operations: Wait (Wait) and Release (Release ). When a thread calls the Wait (Wait) operation, it either reduces the semaphore by one or waits until the semaphore is greater than one or times out. Release (Release) is actually an addition operation on the semaphore, corresponding to the vehicle leaving the parking lot. This operation is called "Release" because the addition operation actually releases the resources protected by the semaphore.

Semaphore (int permits, boolean fair) // create Semaphore with the given number of licenses and given fair settings.

You can also set whether the semaphore adopts the fair mode. If it is executed in fair mode, the thread will be executed in the FIFO order of arrival. If it is not fair, then, the request may be placed in the queue's header.

Use

Semaphore can control the number of resources that can be accessed at the same time, and obtain a license through acquire (). If not, wait, and release () releases a license.

Import java. util. concurrent. executorService; import java. util. concurrent. executors; import java. util. concurrent. semaphore; public class SemaphoreTest {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 20 clients to access for (int index = 0; index <50; 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 semp after access. release (); // availablePermits () indicates the number of available systems in the current traffic signal library. out. println ("-----------------" + semp. availablePermits ();} catch (InterruptedException e) {e. printStackTrace () ;}}; exec.exe cute (run) ;}// exit the thread pool exec. shutdown ();}}
Accessing: 0Accessing: 1Accessing: 2Accessing: 4Accessing: 6Accessing: 8-----------------0-----------------1Accessing: 3-----------------1Accessing: 5Accessing: 9-----------------0-----------------1Accessing: 7Accessing: 10-----------------0-----------------1Accessing: 11-----------------1Accessing: 12-----------------1Accessing: 13Accessing: 14-----------------0-----------------1Accessing: 15-----------------0Accessing: 16-----------------1Accessing: 17-----------------1Accessing: 18-----------------1Accessing: 19-----------------0Accessing: 20Accessing: 21-----------------0Accessing: 22-----------------0-----------------1Accessing: 23-----------------1Accessing: 24-----------------0Accessing: 25Accessing: 26-----------------0-----------------1Accessing: 27-----------------1Accessing: 28-----------------1Accessing: 29Accessing: 30-----------------0-----------------1Accessing: 31-----------------1Accessing: 32-----------------1Accessing: 33-----------------1Accessing: 34Accessing: 35-----------------0-----------------1Accessing: 36-----------------1Accessing: 37-----------------1Accessing: 38-----------------1Accessing: 39-----------------1Accessing: 40Accessing: 41-----------------0-----------------1Accessing: 42Accessing: 43-----------------0Accessing: 44-----------------0-----------------1Accessing: 45-----------------1Accessing: 46-----------------1Accessing: 47-----------------1Accessing: 48-----------------1Accessing: 49-----------------1-----------------2-----------------3-----------------4-----------------5
I am the dividing line of tiantiao

 

 

Reference: http://www.cnblogs.com/linjiqin/archive/2013/07/25/3214676.html

Related Article

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.