Class CountDown

  • All Implemented Interfaces:
    Sync

    public class CountDown
    extends java.lang.Object
    implements Sync
    A CountDown can serve as a simple one-shot barrier. A Countdown is initialized with a given count value. Each release decrements the count. All acquires block until the count reaches zero. Upon reaching zero all current acquires are unblocked and all subsequent acquires pass without blocking. This is a one-shot phenomenon -- the count cannot be reset. If you need a version that resets the count, consider using a Barrier.

    Sample usage. Here are a set of classes in which a group of worker threads use a countdown to notify a driver when all threads are complete.

     class Worker implements Runnable { 
       private final CountDown done;
       Worker(CountDown d) { done = d; }
       public void run() {
         doWork();
        done.release();
       }
     }
     
     class Driver { // ...
       void main() {
         CountDown done = new CountDown(N);
         for (int i = 0; i < N; ++i) 
           new Thread(new Worker(done)).start();
         doSomethingElse(); 
         done.acquire(); // wait for all to finish
       } 
     }
     

    [ Introduction to this package. ]

    • Constructor Summary

      Constructors 
      Constructor Description
      CountDown​(int count)
      Create a new CountDown with given count value
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void acquire()
      Wait (possibly forever) until successful passage.
      boolean attempt​(long msecs)
      Wait at most msecs to pass; report whether passed.
      int currentCount()
      Return the current count value.
      int initialCount()
      Return the initial count value
      void release()
      Decrement the count.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • initialCount_

        protected final int initialCount_
      • count_

        protected int count_
    • Constructor Detail

      • CountDown

        public CountDown​(int count)
        Create a new CountDown with given count value
    • Method Detail

      • acquire

        public void acquire()
                     throws java.lang.InterruptedException
        Description copied from interface: Sync
        Wait (possibly forever) until successful passage. Fail only upon interuption. Interruptions always result in `clean' failures. On failure, you can be sure that it has not been acquired, and that no corresponding release should be performed. Conversely, a normal return guarantees that the acquire was successful.
        Specified by:
        acquire in interface Sync
        Throws:
        java.lang.InterruptedException
      • attempt

        public boolean attempt​(long msecs)
                        throws java.lang.InterruptedException
        Description copied from interface: Sync
        Wait at most msecs to pass; report whether passed.

        The method has best-effort semantics: The msecs bound cannot be guaranteed to be a precise upper bound on wait time in Java. Implementations generally can only attempt to return as soon as possible after the specified bound. Also, timers in Java do not stop during garbage collection, so timeouts can occur just because a GC intervened. So, msecs arguments should be used in a coarse-grained manner. Further, implementations cannot always guarantee that this method will return at all without blocking indefinitely when used in unintended ways. For example, deadlocks may be encountered when called in an unintended context.

        Specified by:
        attempt in interface Sync
        Parameters:
        msecs - the number of milleseconds to wait. An argument less than or equal to zero means not to wait at all. However, this may still require access to a synchronization lock, which can impose unbounded delay if there is a lot of contention among threads.
        Returns:
        true if acquired
        Throws:
        java.lang.InterruptedException
      • release

        public void release()
        Decrement the count. After the initialCount'th release, all current and future acquires will pass
        Specified by:
        release in interface Sync
      • initialCount

        public int initialCount()
        Return the initial count value
      • currentCount

        public int currentCount()
        Return the current count value. This is just a snapshot value, that may change immediately after returning.