Countdownlatch allows one or more threads to wait for other threads to complete the operation.
If we have this requirement: we need to parse multiple sheet in an Excel file, we can consider using multi-threading, each thread resolves a sheet table in Excel, and after all the threads have completed parsing, the program prompts parsing to complete and output the parsing results. To implement this requirement, the simplest way is to use the thread class's join method, waiting for all threads to parse and then prompt for parsing to complete, we can use the code to complete the parsing:
Public classReadexcelsheetsuserthreadjointest { Public Static voidMain (string[] args) throws interruptedexception {Thread Sheet1thread=NewThread (NewRunnable () {@Override Public voidrun () {System. out. println ("parsing Sheet1"); } }); Thread Sheet2thread=NewThread (NewRunnable () {@Override Public voidrun () {System. out. println ("parsing Sheet2"); } }); Sheet1thread.start (); Sheet2thread.start (); Sheet1thread.join (); Sheet2thread.join (); System. out. Print ("all the table parsing completed"); }}
Output Result:
Parse Sheet1 parse Sheet2 all table parsing complete
Join is used to let the current thread of execution wait for the join thread to finish, and it does so by checking to see if the join thread survives and if the join thread survives, let the current thread wait forever. The code is as follows, where wait (0) means waiting forever.
while (IsAlive ()) { Wait (0);}
Until the join thread terminates, the thread's This.notifyall () method is called, and you can refer to the JVM source code to see the process that the Notifyall method was called.
After JDK1.5, Java's concurrency package provides countdownlatch,countdownlatch with the ability to join threads and provides more functionality than the thread join method, and the code that uses Countdownlatch to implement the above requirements is as follows:
Public classReadexcelsheetsusecountdownlatchtest {Private StaticCountdownlatch Countdownlatch =NewCountdownlatch (2); Public Static voidMain (string[] args) throws interruptedexception {Thread Sheet1thread=NewThread (NewRunnable () {@Override Public voidrun () {System. out. println ("parsing Sheet1"); Countdownlatch.countdown (); } }); Thread Sheet2thread=NewThread (NewRunnable () {@Override Public voidrun () {System. out. println ("parsing Sheet2"); Countdownlatch.countdown (); } }); Sheet1thread.start (); Sheet2thread.start (); Countdownlatch.await(); System. out. Print ("all the table parsing completed"); }}
The Countdownlatch constructor takes a parameter of type int as a counter, and if you want to wait for n points to complete, pass in N, when we call Countdownlatch's Countdown method, n is reduced by 1. The await method blocks the current thread until n becomes 0, since Countdownlatch's Countdown method can be used anywhere, so n can be n threads or n steps in 1 threads. When programming, you only need to upload the Countdownlatch reference to the thread.
Sometimes, we do not want the thread of execution to wait until this time we can use Countdownlatch's await (long time,timeunit unit), which will no longer block the execution thread after a certain time. Of course joins also have similar overloaded methods.
The counter must be greater than or equal to 0, just equal to 0, the counter is 0, and the await method is called without blocking the current thread. Countdownlatch It is not possible to reinitialize or modify the value of the internal counter of the Countdownlatch object. One thread calls the Countdown method Happen-before, and another thread calls the await method.
Countdownlatch of Java Concurrency Tool class