title: Awaiting multiple instances of CountDownLatch in container
body: <p>I wrote a class <code>CountDownLatchContainer</code>  it has a <code>await()</code> method, that waits on all CountDownLatches. Everything is working fine.</p>
  
  <pre><code>public final class CountDownLatchContainer
  {
    private final Set&lt;CountDownLatch&gt; countDowns;
  
    public CountDownLatchContainer(List&lt;CountDownLatch&gt; countDowns)
    {
      this.countDowns = new HashSet&lt;CountDownLatch&gt;(countDowns);
    }
  
    public void await() throws InterruptedException
    {
      for (CountDownLatch count : countDowns)
        count.await();
    }
  }
  </code></pre>
  
  <p>For the sake of science and art (:D), I wanted to extend the functionality and add the <code>public boolean await(long timeout, TimeUnit unit)</code> from the <code>CountDownLatch</code> class.</p>
  
  <p>I want each countdown latch to have the same timeout and the method overall just to block for the number of <code>timeout</code> TimeUnits. I have difficulties achieving that. What I have is:</p>
  
  <pre><code>  public boolean await(long timeout, TimeUnit unit) throws InterruptedException
    {
      boolean success = true;
      for (CountDownLatch count : countDowns)
        success &amp;= count.await(timeout / countDowns.size(), unit);
      return success;
    }
  </code></pre>
  
  <p>So, the overall time out is taken care of, but each count down has just a percentage of the overall time. </p>
  
  <p><strong>So how can I give the single latches the same amount of time as specified in the method parameter without execeeding the total time?</strong></p>
  
  <p>Here a visualization. Thx to hexafraction for the ascii art inspiration.</p>
  
  <pre><code>|-----------------------------|
               Total
  |-----------------------------|
    L1       
  |-----------------------------|
    L2       
  |-----------------------------|
  </code></pre>