title: Making specific threads wait until a state's counter reaches 0
body: <p>I'm using a HashMap to keep track of each state's counter and I'm trying to make certain threads that call await(State s) with the same state block until the state's counter is 0. Each thread uses decrementCounter(State S) and then calls await(State s)</p>
  
  <p>I thought about adding a condition to my lock but i dont think it makes sense since i could have two different sets of threads waiting on two counters and when a thread uses signalAll() for one of the counters it wakes up both sets.
  Any hints would be greatly appreciated.</p>
  
  <p>Note: the counter is being incremented in another part of my code but is not relevant.</p>
  
  <p>In the worker thread: </p>
  
  <pre><code>    private final static Counter counter = new Counter();
      if (condition) {
          counter.decrementCounter(s);
          counter.await(s);
      }
  </code></pre>
  
  <p>Counter class:</p>
  
  <pre><code>public class Counter{
      private HashMap&lt;State, Integer&gt; count = new HashMap&lt;State, Integer&gt;();
      private final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
      private final Lock writeLock = lock.writeLock();
      private final Lock readLock = lock.readLock();
  
      public void await(State s) throws InterruptedException {
          readLock.lock();
          try {
              //check if state s counter is 0. if it's not 0 block until it is.
          } finally {
              readLock.unlock();
          }
      }
  
      public void decrementCounter(State s) {
          writeLock.lock();
          try {
              int temp = count.get(s);
              if (temp &gt; 0) {
                  count.put(s, temp - 1);
              }
          } finally {
              writeLock.unlock();
          }
      }
  }
  </code></pre>