title: How to implement a semaphore
body: <p>So I've created a program, trying to display the dangers of using shared variables, so I have 3 classes the main called <code>DangersOfSharedVariables</code> and an <code>Incrementer</code> and <code>Decrementer</code> class.</p>
  
  <p>So the idea is to have two threads running at once, both calling their respected methods, so the <code>Decrementer</code> class will call the <code>decrementShared()</code> method in the main and the <code>Incrementer</code> class will call the <code>incrementShared()</code> method in the main.</p>
  
  <p><strong>Here's the main method:</strong></p>
  
  <pre><code>/*
   * To change this license header, choose License Headers in Project Properties.
   * To change this template file, choose Tools | Templates
   * and open the template in the editor.
   */
  package dangersofsharedvariables;
  
  import java.util.logging.Level;
  import java.util.logging.Logger;
  
  /**
   *
   */
  public class DangersOfSharedVariables {
  
      /**
       * @param args the command line arguments
       */
      private static int sharedValue =0;
      private static int numberOfCycles = 2000000000;
  
      public static void main(String[] args) {
          // TODO code application logic here
          Incrementer in = new Incrementer(numberOfCycles);
          Decrementer de = new Decrementer(numberOfCycles);
          Semaphore sem = new Semaphore(1);
  
  
          in.start();
          try {
              in.join();
          } catch (InterruptedException ex) {}
          de.start();
          try {
              de.join();
          } catch (InterruptedException ex) {}
          System.out.println(sharedValue);
      }
  
      public void decrementShared(){
          sharedValue -=10;
      }
  
      public void incrementShared(){
          sharedValue +=10;
      }
  
  }
  </code></pre>
  
  <p><strong>Here's the Incrementer class</strong></p>
  
  <pre><code>/*
   * To change this license header, choose License Headers in Project Properties.
   * To change this template file, choose Tools | Templates
   * and open the template in the editor.
   */
  package dangersofsharedvariables;
  
  /**
   *
   * 
   */
  public class Incrementer extends Thread {
  
      private int numberOfIncrements;
  
      public Incrementer(int numberOfIncrements) {
          this.numberOfIncrements = numberOfIncrements;
  
      }
  
      @Override
      public void run() {
          DangersOfSharedVariables in = new   DangersOfSharedVariables();
          for(int i = 0; i &lt; numberOfIncrements; i++){
              in.incrementShared();
          }
      }
  }
  </code></pre>
  
  <p><strong>Decrementer Class:</strong></p>
  
  <pre><code>/*
   * To change this license header, choose License Headers in Project Properties.
   * To change this template file, choose Tools | Templates
   * and open the template in the editor.
   */
  package dangersofsharedvariables;
  
  /**
   *
   * 
   */
  public class Decrementer extends Thread {
      private int numberOfDecrements;
      public Decrementer(int numberOfDecrements){
          this.numberOfDecrements = numberOfDecrements;
  
      }
  
      @Override
      public void run(){
          DangersOfSharedVariables d = new DangersOfSharedVariables();
          for(int i = 0; i &lt; numberOfDecrements; i++){
              d.decrementShared();
          }
      }
  }
  </code></pre>
  
  <p>I was googling and a more secure way to do this would be with the use of a Sempaphore class. So I took it upon myself to play around with a semaphore template I found, but am unsure as to how i'd implement it.</p>
  
  <p><strong>Semaphore Class:</strong></p>
  
  <pre><code>package dangersofsharedvariables;
  
  public class Semaphore {
  
      // *************************************************************
      // Class properties.
      // Allow for both counting or mutex semaphores.
      private int count;
  
      // *************************************************************
      // Constructor
      public Semaphore(int n) {
          count = n;
      }
  
      // *************************************************************
      // Public class methods.
      // Only the standard up and down operators are allowed.
      public synchronized void down() {
  
          while (count == 0) {
  
              try {
                  wait(); // Blocking call.
              } catch (InterruptedException exception) {
                  exception.printStackTrace();
              }
          }
          count--;
      }
  
      public synchronized void up() {
          count++;
          notify();
      }
  
  }
  </code></pre>