This is described in the API. Semaphore
Semaphore is typically used to limit the number of threads that can access certain resources (physical or logical).
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 the number of available licenses is counted and action is taken accordingly.
For example, the following class uses semaphores to control the number of threads concurrently
Import Java.util.concurrent.executorservice;import Java.util.concurrent.executors;import Java.util.concurrent.semaphore;public class Testsemaphore {/** * @param args */public static void main (string[] args) { Executorservice pool = Executors.newcachedthreadpool (); Final Semaphore sp = new Semaphore (3,true); for (int i=0;i<10;i++) {Runnable Runnable = new Runnable () {@Overridepublic void run () { try {sp.acquire ();} catch (Interruptedexception e) {e.printstacktrace (); } System.out.println (Sp.availablepermits ()); SYSTEM.OUT.PRINTLN ("Thread" + thread.currentthread (). GetName () + "Enter, already" + (3-sp.availablepermits ()) + "concurrency"); try {thread.sleep ((long) (Math.random () *3000)),} catch (Interruptedexception e) {e.printstacktrace ();} SYSTEM.OUT.PRINTLN ("Thread" +thread.currentthread (). GetName () + "about to leave"); Sp.release (); SySTEM.OUT.PRINTLN ("Thread" +thread.currentthread (). GetName () + "left, already" + (3-sp.availablepermits ()) + "concurrency");}; Pool.execute (runnable); }}}
Again, for example, you can control the thread access resources by the semaphore:
Import Java.util.concurrent.semaphore;public class Downloadthread {private static int in_index = 0;private static int out_ index = 0;private static int buffer_count = 100;public static Boolean g_downloadcomplete;private static Semaphore G_sefull = new Semaphore (0);p rivate static Semaphore g_seempty = new Semaphore (buffer_count); public static Boolean getblockfromnet (int in_index) {int i = 0; while (I < 10000) i++; if (In_index < buffer_count-1) return false; else return true; } public static void Writeblocktodisk (int out_index) {int i = 0; while (I < 100000) i++; }/** * @param args */public static void main (string[] args) {g_downloadcomplete = false; Thread Threada = new Thread () {public void run () {ProA (); } }; Thread threadb = new Thread () {public void run () {ProB (); } }; Threadb.start (); Threada.start (); } public static void ProA () {while (G_sefull.availablepermits () < Buffer_count) {try {g_seempty.acquire () ; } CATCH (interruptedexception E1) {//TODO auto-generated catch block E1.printstacktrace (); } G_downloadcomplete = Getblockfromnet (In_index); In_index = (in_index + 1)% Buffer_count; G_sefull.release (); System.out.println ("Download a block" + in_index); if (g_downloadcomplete) break; }}public static void ProB () {while (g_seempty.availablepermits () > 0) {try {g_sefull.acquire (); } catch (Interruptedexception E1) {//TODO auto-generated catch block E1.printstacktrace (); } writeblocktodisk (Out_index); Out_index = (out_index + 1)% Buffer_count; G_seempty.release (); System.out.println ("Write a block" + out_index); if (g_downloadcomplete && out_index = = in_index) break; }}}
How to control the number of threads accessing resources in Java