Latching: a synchronous helper class that allows one or more threads to wait until a set of operations that are performed in another thread is Completed. That is, a set of threads waits for an event to occur, all threads will block waiting before the event occurs, and all threads will start executing after the event occurs, the latch is initially closed, the latch will be opened when the event occurs, and the latch will always be open once it is Opened.
1 Importjava.io.BufferedReader;2 Importjava.io.IOException;3 Importjava.io.InputStream;4 Importjava.io.InputStreamReader;5 Importjava.net.URL;6 Importjava.net.URLConnection;7 Importjava.util.ArrayList;8 Importjava.util.List;9 Importjava.util.concurrent.CountDownLatch;Ten one public classThreadTest { a public Static voidmain (string[] Args) { - Final intnum = 10; - //the Countdownlatch is a flexible latching implementation that includes a counter, which is initialized with a positive number, indicating how many events need to wait. the FinalCountdownlatch begin =NewCountdownlatch (1); - FinalCountdownlatch end =NewCountdownlatch (num); - for(inti = 0; I < num; i++) { - NewThread (Newmyworker (i, begin, end)). start (); + } - //Sleep 10 seconds, Wait until all threads are ready, and then start the Concurrency Test. + Try { aThread.Sleep (10000); at}Catch(interruptedexception E1) { - e1.printstacktrace (); - } -System.out.println ("start a concurrency test"); - //calling the Countdown method decrements the calculator to indicate that an event has occurred - Begin.countdown (); in LongStartTime =System.currenttimemillis (); - Try { to //the await method is blocked until the counter is zero, or the waiting thread is interrupted, timed out + end.await (); -}Catch(interruptedexception E) { the e.printstacktrace (); *}finally { $ LongEndTime =System.currenttimemillis ();Panax NotoginsengSystem.out.println ("end concurrency Test!")); -System.out.println ("time spent:" + (endTime-startTime)); the } + } a } the + classMyworkerImplementsRunnable { - FinalCountdownlatch begin; $ FinalCountdownlatch end; $ Final intid; - - publicMyworker (Final intIdFinalCountdownlatch begin, the FinalCountdownlatch End) { - this. ID =id;Wuyi this. Begin =begin; the this. end =end; - } wu - @Override about public voidRun () { $ Try { -System.out.println ( this. ID + "ready!"); - //the await method is blocked until the counter is zero, or the waiting thread is interrupted, timed out - begin.await (); a //Execute your logic +URLConnection weburlrequest = Request ("http://the090303.com"); thelist<string> ReadLine =readLine (weburlrequest.getinputstream ()); - //System.out.println (readline); $Thread.Sleep ((Long) (math.random () * 10000)); the}Catch(throwable E) { the e.printstacktrace (); the}finally { theSystem.out.println ( this. ID + "complete test!"); - //calling the Countdown method decrements the calculator to indicate that an event has occurred in End.countdown (); the } the } about the public StaticURLConnection Request (String Requesturl)throwsIOException { the return NewURL (requesturl). openconnection (); the } + - public StaticList<string>readLine (inputstream Inputstream) the throwsIOException {Bayilist<string> list =NewArraylist<string>(); theBufferedReader Bufferreader =NULL; the Try { -Bufferreader =NewBufferedReader ( - NewInputStreamReader (inputstream)); the String readline; the while((readline = Bufferreader.readline ())! =NULL) { the List.add (readline); the } -}finally { the bufferreader.close (); the } the returnlist;94 } the}
The above countdownlatch is a flexible latching implementation that includes a counter, which is initialized with a positive number, indicating how many events need to wait. Call the countdown method decrement calculator, which indicates that an event has occurred, and the await method is blocked until the counter is zero, or the waiting thread is interrupted, Timeout. so the above with two latching to achieve a starting gate (begin) with the end of the gate (end), the starting gate counter initial value is 1, the end of the gate calculator is the number of threads, each thread is waiting at the start gate, so to ensure true concurrency, and each thread in the last thing is to call the end of the count The down method is reduced by 1 so that when all the threads are executed, we can count the time spent by all threads accessing the Author's Website.
Implementing concurrency with Java latching