A CountDownLatch (down counter) Description - Concurrent

Method Description:
 
public void countDown()
     Latch count is decremented, if the count reaches zero, releasing all waiting threads. If the current count is greater than zero, the count will be reduced. If the new count is zero, for thread scheduling purposes, will re-enable all waiting threads.
     If the current count is equal to zero, nothing happens.

public boolean await (long timeout, TimeUnit
unit) throws InterruptedException      Causes the current thread to wait before been counted down to zero latch, unless the thread is interrupted, or the specified waiting time. If the current count is zero, this method returns true value immediately.
     If the current count is greater than zero, then for thread scheduling purposes, the current thread is disabled, and before one of three things happens, the thread lies dormant until:
         Since the call countDown () method, count reaches zero; or some other thread interrupts the current thread; or has exceeded the specified wait time.
          * If the count reaches zero, then the method returns a true value.
          * If the current thread, on entry to this method has been set interrupt status of the thread; or is interrupted while waiting, then throws InterruptedException, and clears the current thread's interrupted status.
          * If you exceed the specified waiting time, the return value is false. If the time is less than zero, then this method will not wait.
    
     parameter:
         The maximum time to wait - timeout
         unit - the time unit of the timeout argument.
     return:
         If the count reaches zero, return true; if the count reaches zero before the wait time elapses, false is returned
     Throws:
          InterruptedException - if the current thread is interrupted while waiting
 
Example 1:
      The main thread waits for the child thread execution is completed in execution.
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
 
public class CountdownLatchTest1 {
 
        public static void main(String[] args) {
              ExecutorService service = Executors. newFixedThreadPool(3);
               final CountDownLatch latch = new CountDownLatch(3);
               for (int i = 0; i < 3; i++) {
                     Runnable runnable = new Runnable() {
                           
                            @Override
                            public void run() {
                                   try {
                                         . System out.println ( . "Child thread" + Thread.currentThread () getName () + " started" );
                                         Thread. sleep((long) (Math. random() * 10000));
                                         . System out.println ( . "Child thread" + Thread.currentThread () getName () + " execution completed" );
                                         latch.countDown (); // current thread calls this method, the count is reduced by a 
                                  } the catch (InterruptedException E) {
                                         e.printStackTrace ();
                                  }
                           }
                     };
                     
              }
              
               try {
                     . System out.println ( . "Main thread" + Thread.currentThread () getName () + " waiting for child thread execution is complete ..." );
                     latch.await (); // blocks the current thread until the timer value 0 
                     System out.println ( "main thread" + Thread.currentThread () getName () + " started ....". );
              } catch (InterruptedException e) {
                     e.printStackTrace ();
              }
         } }
Example 2:
     Meter race, four athletes waiting for players to reach the venue password referee, the referee heard the password, after hearing the players start at the same time, when all the players reach the end, the referee aggregated summary rankings.
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
 
public class CountdownLatchTest2 {
 
        public static void main(String[] args) {
              ExecutorService service = Executors. newCachedThreadPool();
               final CountDownLatch cdOrder = new CountDownLatch(1);
               final CountDownLatch cdAnswer = new CountDownLatch(4);
               for (int i = 0; i < 4; i++) {
                     Runnable runnable = new Runnable() {
                            public void run() {
                                   try {
                                         . System out.println ( . "Contestant" + Thread.currentThread () getName () + " is waiting for the referee issued password" );
                                         cdOrder.await();
                                         . Out.println System ( "player" + Thread.currentThread () getName () + " has accepted the referee password." );
                                         Thread. sleep((long) (Math. random() * 10000));
                                         . Out.println the System ( "player" + Thread.currentThread () getName () + " to reach the terminal." );
                                         cdAnswer.countDown();
                                  } catch (Exception e) {
                                         e.printStackTrace ();
                                  }
                           }
                     };
                     service.execute(runnable);
              }
               try {
                     Thread. sleep((long) (Math. random() * 10000));
 
                     . Out.println System ( "referee" + Thread.currentThread () .getName () + " is about to release the password" );
                     cdOrder.countDown();
                     Out.println System (. "Referee" + Thread.currentThread () .getName () + " password has been sent, waiting for all players reach the end" );
                     cdAnswer.await();
                     . Out.println System ( "All the players reach the end" );
                     . Out.println System ( "referee" + Thread.currentThread () .getName () + " Summary standings" );
              } catch (Exception e) {
                     e.printStackTrace ();
              }
              service.shutdown();
 
       }
}

 

Guess you like

Origin www.cnblogs.com/wsy0202/p/12024701.html