title: How to use Semaphores for Signaling?
body: <p>Now I investigate semaphores. I googled following link about this theme:</p>
  
  <p><a href="http://tutorials.jenkov.com/java-concurrency/semaphores.html" rel="nofollow">link</a></p>
  
  <p>Author of this link wrote about using semaphores for signaling. To show how it works he wrote custom semaphore.</p>
  
  <p>custom semaphore code:</p>
  
  <pre><code>public class Semaphore {
    private boolean signal = false;
  
    public synchronized void take() {
      this.signal = true;
      this.notify();
    }
  
    public synchronized void release() throws InterruptedException{
      while(!this.signal) wait();
      this.signal = false;
    }
  
  }
  </code></pre>
  
  <p>about how use it in code he wrote following:</p>
  
  <pre><code>public class SendingThread {
    Semaphore semaphore = null;
  
    public SendingThread(Semaphore semaphore){
      this.semaphore = semaphore;
    }
  
    public void run(){
      while(true){
        //do something, then signal
        this.semaphore.take();
  
      }
    }
  }
  
  
  
  public class RecevingThread {
    Semaphore semaphore = null;
  
    public ReceivingThread(Semaphore semaphore){
      this.semaphore = semaphore;
    }
  
    public void run(){
      while(true){
        this.semaphore.release();
        //receive signal, then do something...
      }
    }
  }
  </code></pre>
  
  <p>main:</p>
  
  <pre><code>Semaphore semaphore = new Semaphore();
  
  SendingThread sender = new SendingThread(semaphore);
  
  ReceivingThread receiver = new ReceivingThread(semaphore);
  
  receiver.start();
  sender.start();
  </code></pre>
  
  <p>As I understood order of execution should be following</p>
  
  <pre><code>send - receive
  send - receive
  send - receive
  ...
  </code></pre>
  
  <p>I tryed  to write own code using this bluerprint</p>
  
  <pre><code>public class SendReceiveWithCustomSemaphore {
      public static void main(String[] args) {
          MySemaphore mySemaphore = new MySemaphore();
          new Send(mySemaphore).start();
          new Receive(mySemaphore).start();
      }
  }
  
  class MySemaphore {
      boolean flag = false;
  
      public synchronized void take() throws InterruptedException {
          flag = true;
          notify();
      }
  
      public synchronized void release() throws InterruptedException {
          while (!flag) {
              wait();
          }
          flag = false;
      }
  }
  
  class Send extends Thread {
      MySemaphore mySemaphore;
  
      public Send(MySemaphore semaphore) {
          this.mySemaphore = semaphore;
      }
  
      @Override
      public void run() {
          int i = 0;
          while (i++ &lt; 10) {
              System.out.println("send");
              try {
                  mySemaphore.take();
              } catch (InterruptedException e) {
                  e.printStackTrace();
              }
          }
      }
  }
  
  class Receive extends Thread {
      MySemaphore mySemaphore;
  
      public Receive(MySemaphore semaphore) {
          this.mySemaphore = semaphore;
      }
  
      @Override
      public void run() {
          while (true) {
              try {
                  mySemaphore.release();
              } catch (InterruptedException e) {
                  e.printStackTrace();
              }
              System.out.println("receive");
          }
      }
  }
  </code></pre>
  
  <p>output:</p>
  
  <pre><code>send
  send
  send
  send
  send
  send
  send
  send
  send
  send
  receive
  </code></pre>
  
  <p>Thus it is not expected behaviour for me.</p>
  
  <p>I made a mistake then I wrote code or I didn't understand concept ?</p>
  
  <p>What did author want to say?</p>