A brief
CountDownLatch is a common tool used to control concurrency, which is a class java.util.concurrent package, provided that the main mechanism CountDownLatch when a plurality (a number equal to the specific value of the count parameter is initialized when CountDownLatch) has reached a thread or when the state is expected to trigger an event expected to complete the work, other threads may be waiting for this event to trigger their follow-up. The state is expected to reach its own thread calls countDown method CountDownLatch, while waiting thread calls await method of CountDownLatch.
CountDownLatch constructor receives as a parameter of type int counter, if you want to wait for the completion of N points, here the incoming N.
If a worker process is relatively slow, we can not let the main thread to wait forever. It is possible to use another method await with a specified time ---- await (long time, TimeTnit unit), developed this method after waiting time, do not block the current thread.
Note : The counter must be greater than or equal to 0, when the counter is equal to 0, calling await method does not block the current thread;
Two, CountDownLatch uses examples
Import java.util.concurrent.CountDownLatch; public class CountDownLatchDemo { public static void main (String [] args) throws InterruptedException { a CountDownLatch LATCH = new new a CountDownLatch (2); // two threads work new new the Worker ( "WORK1", 200 is , LATCH) .start (); new new Worker ( "WORK2", 300 , LATCH) .start (); latch.await (); // wait for all threads to finish System.out.println ( "work completed" ); } static class the Worker the extends the Thread { WorkName String; int WorkTime; a CountDownLatch LATCH; public the Worker (String workName, int WorkTime, a CountDownLatch LATCH) { the this .workName = workName; the this .workTime = WorkTime; the this .latch = LATCH; } public void RUN () { the System.out .println (workName + "start Job" ); the doWork (); System.out.println (workName + "end Job" ); latch.countDown (); // work done, a counter is decremented } private void doWork() { try { Thread.sleep(workTime); } catch (InterruptedException e) { e.printStackTrace(); } } } }
Third, compared to join the
Compared to the same child can make a thread wait for the thread to complete the join, CountDownLatch counter provided by a more flexible control, call countDown method, N will be minus 1, CountDownLatch the await method blocks the current thread, knowing N becomes 0 Since countDown method can be used anywhere, so here to say N points can be N threads, it can also be N execution steps.
The join is used to make the current thread wait JOIN thread execution ends. Implementation principle is constantly checking JOIN thread is alive, if JOIN thread alive then let the current thread to wait forever.
Examples are as follows:
Import java.util.concurrent.CountDownLatch; public class CountDownLatchDemo { public static void main (String [] args) throws InterruptedException { a CountDownLatch LATCH = new new a CountDownLatch (2); // two threads work new new the Worker ( "WORK1", 200 is , LATCH) .start (); new new Worker ( "WORK2", 300 , LATCH) .start (); latch.await (); // wait for all threads to finish System.out.println ( "work completed" ); } static class the Worker the extends the Thread { WorkName String; int WorkTime; a CountDownLatch LATCH; public the Worker (String workName, int WorkTime, a CountDownLatch LATCH) { the this .workName = workName; the this .workTime = WorkTime; the this .latch = LATCH; } public void RUN () { the System.out .println (workName + "start Job" ); the doWork (); System.out.println (workName + "end Job" ); latch.countDown (); // work done, a counter is decremented System.out.println (workName + "start the second phase" ); the doWork (); System.out.println (workName + "end of the second phase" ); } Private void the doWork () { the try { the Thread.sleep (WorkTime); } the catch (InterruptedException E) { e.printStackTrace (); } } } }
Print results are as follows:
work1 start working
work2 start working
work1 end of the work
work1 start of the second phase of
work2 end of the work
the work completed
work2 start of the second phase of
work1 end of the second phase of
work2 end of the second phase of work