Class WaitGroup

java.lang.Object
io.javago.sync.WaitGroup
All Implemented Interfaces:
AutoCloseable

public class WaitGroup extends Object implements AutoCloseable
The WaitGroup class implements Go's sync.WaitGroup. A synchronization aid that allows one or more threads to wait until a set of operations being performed in other threads completes. It implements the AutoCloseable interface and allows a try-with-resources statement to automatically decrease its count by one.
  • Constructor Summary

    Constructors
    Constructor
    Description
    Constructs a new WaitGroup with an initial count of zero.
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    add(int amount)
    Increments the count of this wait group by the specified amount.
    void
    Causes the current thread to wait until the count of this wait group reaches zero.
    void
    Decrements the count of this wait group by one.
    void
    Decrements the count of this wait group by one.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • WaitGroup

      public WaitGroup()
      Constructs a new WaitGroup with an initial count of zero.
  • Method Details

    • add

      public void add(int amount)
      Increments the count of this wait group by the specified amount.
      Parameters:
      amount - the amount by which to increment the count
    • done

      public void done()
      Decrements the count of this wait group by one. If the count reaches zero, all waiting threads are notified. Equivalent to close().
      Throws:
      IllegalStateException - if the wait group has already reached zero
    • await

      public void await()
      Causes the current thread to wait until the count of this wait group reaches zero. If the current count is zero, this method returns immediately.
    • close

      public void close()
      Decrements the count of this wait group by one. If the count reaches zero, all waiting threads are notified. Equivalent to done().
      Specified by:
      close in interface AutoCloseable
      Throws:
      IllegalStateException - if the wait group has already reached zero