The semaphore class is also java1.5, located under the Java.util.concurrent package
First look at his document explanation:
A count semaphore. Conceptually, semaphores maintain a set of licenses. If necessary, each one is blocked before the license is available acquire()
, and then the license is acquired. Each release()
add a license, which may release a blocked fetch. However, instead of using the actual license object, Semaphore
only count the number of available licenses and take appropriate action, he has two very important methods, one is acquire, the other is the release method
Now look at the introduction of these two methods
Acquire () throws Interruptedexception
-
Obtain a license from this semaphore to block the thread until a license is provided, or the thread is interrupted. Gets a license (if one is provided) and returns immediately, minus 1 of the available licenses.
If no licenses are available, it is forbidden to use the current thread for thread scheduling purposes and put it in hibernation until one of the following two scenarios occurs:
- Some other threads call this semaphore
release()
method, and the current thread is the next thread to be assigned a license, or
- Some other threads interrupt the current thread.
If the current thread:
- Set its interrupted state to on by this method, or
- When waiting for permission to be
中断
.
is thrown InterruptedException
, and the current thread's interrupted state is cleared.
-
-
-
-
-
Thrown:
-
InterruptedException
-If the current thread is interrupted
Release ()
-
Release a license to return it to the semaphore. Release a license and increase the number of licenses available by 1. If any thread attempts to obtain a license, select a thread and give it the license that was just released. The thread is then enabled (or re-enabled) for thread scheduling purposes.
A thread that does not require the release of a license must be called acquire()
to obtain a license. The correct usage of semaphores is established by programming conventions in the application.
Now write an example to verify the next
The example of a polygon allows only 5 threads to enter the code between acquire () and release () at the same time
Package Cn.kge.com.thread;import Java.util.concurrent.executorservice;import java.util.concurrent.Executors; Import Java.util.concurrent.semaphore;public class Threadsemaphoretest {public static void main (string[] args) {//thread pool Executorservice exec = Executors.newcachedthreadpool (); Only 5 threads can access the final Semaphore semp = new Semaphore (5) at a time; Simulates 20 client access for (int index = 0; index <; index++) {final int NO = index; Runnable run = new Runnable () {public void run () {try {/ /Get License Semp.acquire (); SYSTEM.OUT.PRINTLN ("Thread" + thread.currentthread (). GetName () + "Enter, currently available"); Thread.Sleep ((Long) (Math.random () * 10000)); After the access, release, if the following statement is masked, the console can only print 5 records, after which the thread has been blocking System.out.println ("thread" + thread.currentthread (). Getna Me () + "about to leave"); Semp.release (); } catch (Interruptedexception e) {}}}; Exec.execute (run); }//Exit thread pool Exec.shutdown (); } }
Results:
Thread Pool-1-thread-1 entered, currently available
Thread Pool-1-thread-2 entered, currently available
Thread Pool-1-thread-4 entered, currently available
Thread Pool-1-thread-6 entered, currently available
Thread Pool-1-thread-5 entered, currently available
Thread pool-1-thread-6 is about to leave
Thread Pool-1-thread-8 entered, currently available
Thread pool-1-thread-4 is about to leave
Thread Pool-1-thread-9 entered, currently available
Thread pool-1-thread-8 is about to leave
Thread Pool-1-thread-10 entered, currently available
Thread pool-1-thread-5 is about to leave
Thread Pool-1-thread-3 entered, currently available
Thread pool-1-thread-1 is about to leave
Thread Pool-1-thread-7 entered, currently available
Thread pool-1-thread-2 is about to leave
Thread Pool-1-thread-13 entered, currently available
Thread pool-1-thread-13 is about to leave
Thread pool-1-thread-12 entered, currently available
Thread pool-1-thread-10 is about to leave
Thread pool-1-thread-11 entered, currently available
Thread pool-1-thread-3 is about to leave
Thread pool-1-thread-14 entered, currently available
Thread pool-1-thread-12 is about to leave
Thread Pool-1-thread-16 entered, currently available
Thread pool-1-thread-7 is about to leave
Thread POOL-1-THREAD-17 entered, currently available
Thread pool-1-thread-9 is about to leave
Thread pool-1-thread-18 entered, currently available
Thread pool-1-thread-16 is about to leave
Thread Pool-1-thread-15 entered, currently available
Thread pool-1-thread-14 is about to leave
Thread pool-1-thread-19 entered, currently available
Thread pool-1-thread-18 is about to leave
Thread pool-1-thread-20 entered, currently available
Thread pool-1-thread-11 is about to leave
Thread pool-1-thread-17 is about to leave
Thread pool-1-thread-15 is about to leave
Thread pool-1-thread-19 is about to leave
Thread pool-1-thread-20 is about to leave
Java Semaphore Semaphore Introduction