Implementing a String pool: Strpool.javaImportjava.util.ArrayList;Importjava.util.List;ImportJava.util.concurrent.Semaphore;Importjava.util.concurrent.locks.Condition;ImportJava.util.concurrent.locks.ReentrantLock; Public classStrpool {Private intPoolmaxsize = 5;//maximum value of pool Private intSemaphorepermits = 5;//allowable number of semaphore Privatelist<string> list =NewArraylist<string>(); PrivateSemaphore Concurrencysemaphore =NewSemaphore (semaphorepermits); PrivateReentrantlock lock =NewReentrantlock (); PrivateCondition Condition =lock.newcondition (); PublicStrpool () {Super(); for(inti = 0; i < poolmaxsize; i++) {List.add ("String" + (i + 1)); } } Publicstring Get () {string getString=NULL; Try{concurrencysemaphore.acquire (); Lock.lock (); while(list.size () = = 0) {condition.await (); } getString= List.remove (0); Lock.unlock (); } Catch(interruptedexception e) {e.printstacktrace (); } returngetString; } Public voidput (String stringvalue) {lock.lock (); List.add (StringValue); Condition.signalall (); Lock.unlock (); Concurrencysemaphore.release (); }}testthread.java Public classTestthreadextendsThread {PrivateStrpool Listpool; PublicTestthread (Strpool listpool) {Super(); This. Listpool =Listpool; } @Override Public voidrun () { for(inti = 0; i < Integer.max_value; i++) {String getstr=Listpool.get (); System.out.println (Thread.CurrentThread (). GetName ()+ "Fetch value:" +getstr); Listpool.put (GETSTR); }}}run.java Public classRun { Public Static voidMain (string[] args) {/*** String pool, actual application can be changed to database connection pool, etc. (own definition)*/Strpool Pool=NewStrpool (); Testthread[] Threadarray=NewTestthread[12]; for(inti = 0; i < threadarray.length; i++) {Threadarray[i]=Newtestthread (pool); } for(inti = 0; i < threadarray.length; i++) {Threadarray[i].start (); }}} Common methods: Construction method: Semaphore (intpermits) creates a Semaphore with the given number of licenses and unfair fairness settings. Semaphore (intPermits,BooleanFair) Creates a Semaphore with the given number of licenses and a given fairness setting. Method:voidacquire () Gets a license from this semaphore, blocking the thread until a license is provided, or the thread is interrupted. voidAcquire (intpermits) Gets a given number of licenses from this semaphore, blocks the thread until the license is provided, or the thread has been interrupted. voidacquireuninterruptibly () Gets a license from this semaphore and blocks it before a license is available. voidAcquireuninterruptibly (intpermits) Gets a given number of licenses from this semaphore, blocking threads until these licenses are available. intavailablepermits () returns the number of licenses currently available in this semaphore. intDrainpermits () Gets and returns all licenses that are available immediately.//for general testingprotectedCollection<thread>getqueuedthreads () returns a collection that contains the threads that may be waiting to be fetched. intGetqueuelength () returns the estimated number of threads that are waiting to be fetched. Booleanhasqueuedthreads () query whether the thread is waiting to be fetched. BooleanIsfair () If the fairness of this semaphore is set totrue, The returntrue. protected voidReducepermits (intreduction) reduces the number of available licenses based on the specified reduction amount. voidrelease () releases a license to return it to the semaphore. voidReleaseintpermits) releases a given number of licenses and returns them to the semaphore. String toString () returns the string that identifies the semaphore and the state of the semaphore. BooleanTryacquire () a license is obtained from the semaphore only if there is one available license for this semaphore at the time of invocation. BooleanTryacquire (intpermits) These licenses are only obtained from this semaphore when there is a given number of licenses in this semaphore at the time of invocation. BooleanTryacquire (intPermits,LongTimeout, timeunit unit) If all licenses are available for this semaphore within a given wait time, and the current thread is not interrupted, a given number of licenses are obtained from this semaphore. BooleanTryacquire (LongTimeout, timeunit unit) If there is a license available for this semaphore within a given wait time and the current thread is not interrupted, obtain a license from this semaphore.
161207, high Concurrency: java.util.concurrent.Semaphore implementation of string pool and its common methods introduction