title: Semaphore deadlock
body: <p>So I have a problem using semaphore.
  Writing a code where are 4 rooms and some visitors. Each room has a certain cap for the amount  of visitors they can hold. So entering a full room would trigger a wait().
  The visitors must not leave a room before they can enter another, so they are always in a room.</p>
  
  <pre><code>public class Semaphore {
  
    private int placesLeft;
  
    public Semaphore(int placesInRoom) {
      this.placesLeft = placesInRoom;
    }
  
    public synchronized void acquire(Visitor visitor) {
      Semaphore sem = visitor.getRoom().getSemaphore();
  
      try {
        while (placesLeft &lt;= 0) {
          this.wait();
      }
  
    } catch (InterruptedException e) {}
  
    sem.release();
    placesLeft--;
  }
  
  public synchronized void release() {
    placesLeft++;
    this.notifyAll();
  }
  </code></pre>
  
  <p>Deadlock appears when 2 people are trying to enter each other's rooms.
  Also for some reason the <code>placesLeft</code> count is not coming out right.</p>
  
  <p>So what should I do?</p>
  
  <p>EDIT:</p>
  
  <p>Been busy with something else, reviving the question.
  The problem doesnt occure because of rooms get full, lock occures when person1 from room1 wants to enter room2 and the same time person2 from room2 wants to enter room1. As I understant its something to do with synchrozing maybe? They get stuck before release, so release is not called. As i understand one rooms accuire and release cannot be called same time. So basicly room1 semaphore release cannot be called cuz on same time the accuire is called, same for room2? I'm newbie coder and synchronizing is not so clear yet. 
  Removing synchronizes from one or another doesnt seem to work (is prolly wrong also).</p>