title: Java monitor implementation
body: <p>I'm trying to do the Unisex Bathroom problem in Java. In short, there is one bathroom with an infinite number of toilets that both men and women can use. Men and women can't be in the bathroom at the same time, but several men or women can. Men and women are represented by threads, and the bathroom by a monitor (shared object with synchronized methods, that monitors and dictates how the concurrency works).</p>
  
  <p>Now to my question. Normally you would write something like:</p>
  
  <pre><code>public synchronized void womanEnter()
  {
     while(menInTheBathroom&gt;0 || menWaiting&gt;0)
     {
        wait();
     }
      //enter bathroom
  }
  </code></pre>
  
  <p>And to wake that process up you notify()
  But I (think I) need to be able to notify men and women separately. If a man enters the bathroom I want him to notify all the men in the queue. I could use a couple of semaphores in the monitor, but that's not allowed in the excersize. Can I use conditions in the monitor that I can wait for and notify, and if I can, how? I tried, but got a bunch of errors involving illegal monitor state.</p>