title: Sleeping barber, semaphores?
body: <p>I've been trying to solve this for two days now and I finally give up, I'm posting my code here with the hope that some fellow human can provide me with what I have been missing, because I think I am close, please help!</p>
  
  <p>Code For Barber: </p>
  
  <pre><code>public Semaphore customer = new Semaphore(0);
  public Semaphore barber = new Semaphore(0);
  public Semaphore accessSeats = new Semaphore(1);
  public int freeChairs = 5;
  
  @Override
  public void run() {
      while (true) {
          try {
              customer.acquire(); //it seems the barber never acuires a customer, even after the semaphore is released
              accessSeats.acquire();
              freeChairs += 1;
              cutHair();
              barber.release();
              accessSeats.release();
          } catch (InterruptedException ex){}
  </code></pre>
  
  <p>Code for Customer:</p>
  
  <pre><code>    public void run() {
      while (notCut) {
          try {
              bs.accessSeats.acquire();
          } catch (InterruptedException ex) {}
          if (bs.freeChairs &gt; 0) {
              bs.freeChairs -= 1;
              bs.customer.release();
              bs.accessSeats.release();
              try {
                  bs.barber.acquire(); //the customer cant get past this line
                  getHaircut();
              } catch (InterruptedException ex) {}
          } else {
              bs.accessSeats.release();
              notCut = false;
          }
      }
  }
  </code></pre>
  
  <p>I create 1 barber and 100 customers on a random timer.</p>
  
  <p>EDIT: <code>cutHair()</code> sleeps the barber thread for 1-5 seconds (random). and <code>getHaircut()</code> just prints a message, and sets <code>notCut=false;</code></p>