title: Semaphores used for signaling in threads not working(concurrency issue)
body: <p>in this project I am trying to do some concurrency among threads using semaphores as signaling, however the concurrency is not working at all. I can only use acquire and release and no synchronized keyword methods allowed. I read countless webpages and it says that </p>
  
  <p>// do something</p>
  
  <p>acquire() </p>
  
  <p>release() </p>
  
  <p>//do something </p>
  
  <p>Which I understand but in this program I am trying to test signals with a semaphore between threads, for example user requests deposit and teller should say deposit completed.However my signals(semaphores) are not working as I want to print in order for example</p>
  
  <p>I need to deposit 
  Deposit is complete </p>
  
  <p>instead I get this </p>
  
  <p>Customer0created</p>
  
  <p>I need to deposit</p>
  
  <p>I have withdrawn &lt;---</p>
  
  <p>Customer0joined from main</p>
  
  <p>Teller0created</p>
  
  <p>You wanna withdrawal? &lt;---- (out of order)</p>
  
  <p>Deposit is complete</p>
  
  <p>Regardless how i reorder them or how much i read the semaphore signaling to comm threads is not working.</p>
  
  <pre><code> [code]import java.util.concurrent.Semaphore;
  
   public class Threads {
  
  
   private static Semaphore depositTransaction = new Semaphore (1, true);
   private static Semaphore withdrawal = new Semaphore (1, true);
  
   public static void main(String[] args) 
  
     {
      final int customerThreads = 1;
      final int tellerThreads = 1;
      final int loanThreads = 1;
  
      Customer thr[] = new Customer[customerThreads]; //  
     Thread cThread[] = new Thread[customerThreads]; //         
      for (int i= 0; i &lt; customerThreads; i++)
      {
          thr[i]= new Customer(i);
          cThread[i] = new Thread(thr [i]);
          cThread[i].start();
  
      }
      for ( int i = 0; i &lt; customerThreads; i++ )
      {
          try {
              cThread[i].join();
              System.out.println("Customer"+i + "joined from main");
  
          }
          catch (InterruptedException e)
          {
  
          }
      }
  
      Teller thr1[] = new Teller[tellerThreads];
      Thread tThread[] = new Thread[tellerThreads];
      for (int b = 0; b&lt; tellerThreads; b++)
      {
          thr1[b] = new Teller(B)/&gt;;
          tThread[b]= new Thread(thr1 [b]);
          tThread[b].start();
      }              
  }   
  
  static class Customer implements Runnable
   {
  private int customerNumber = 0;
  private int balance = 0;
  
  Customer(int cn)
  {
     this.customerNumber = cn;
     balance = 1000;
     System.out.println("Customer"+ customerNumber + "created");
  }
  
  public void run()
  {
  
      try
      {
  
       System.out.println("I need to deposit");
      depositTransaction.acquire();// signal
  
      }
      catch(InterruptedException e)
      {
          Thread.currentThread().interrupt();
          e.printStackTrace();
      }
  
     withdrawal.release();
     System.out.println("I have withdrawn");
  }       
  }
  
  static class Teller implements Runnable 
  {
  private int tellerNumber = 0;
  
  Teller(int tn)
  {
      this.tellerNumber = tn;
      System.out.println("Teller"+ tellerNumber +"created");
  }
  
  public void run()
  {
      try 
      {
  
      System.out.println("You wanna withdrawal?"); 
      withdrawal.acquire();
  
      }
      catch(InterruptedException e)
      {
          Thread.currentThread().interrupt();
  
      }
      depositTransaction.release();
      System.out.println("Deposit is complete");
  }
  
  } 
  
  }[/code]
  </code></pre>