title: Java - group threads in one monitor
body: <p>I have a monitor class, where threads while executing a certain method, are supposed to wait until a certain value is false in a <code>BiPredicate&lt;Integer, Integer&gt;</code>. Also, there is a class that sets values in that predicate to false. It looks something like this:</p>
  
  <pre><code>class Monitor {
      synchronized void doSomething(int x, int y) {
          //...
          while(pred.test(x, y)) {
              wait();
          }
          //...
      }
      synchronized void setFalse(int a, int b) {
          pred.and((x, y) -&gt; x != a &amp;&amp; y != b);
          notifyAll();
      }
  }
  </code></pre>
  
  <p>Is there a way to make it so that only threads that wait for specifically <code>a</code> and <code>b</code> are notified, and not all threads waiting on the monitor? One solution I thought of was to create a HashMap of helper objects with pairs of Integers as keys and <code>doSomething()</code> would wait on those objects and then <code>setFalse()</code> would check if the Map contained a given pair and only notify those that wait for the helper object corresponding to it. But this approach complicates <code>doSomething()</code>'s locks, as if I gained a lock on the helper object I wouldn't be able to release my lock on the Monitor itself. Maybe I could use semaphores for that, but that would require creation and destruction of lots of Semaphores on the fly, as keeping them in memory is unfeasible. What is the best approach here?</p>