title: How to prevent thread starvation in java
body: <p><strong>I have Written program which solves the below problem but I am trying to make it STARVATION FREE but I am not sure how to implement it.  So,  each north farmers and south farmers get an equal amount of chance to cross the bridge.</strong> </p>
  
  <p><strong>Scenario</strong> 
  A single-lane bridge connects the two Vermont villages of North Tunbridge and South Tunbridge. Farmers in the two villages use this bridge to deliver their produce to the neighboring town. The bridge can become deadlocked if both a northbound and a southbound farmer get on the bridge at the same time (Vermont farmers are stubborn and are unable to back up.)</p>
  
  <p><strong>Here what I have tried:</strong></p>
  
  <pre><code>package threading.practice;
  
  import java.util.concurrent.Semaphore;
  import java.util.concurrent.TimeUnit;
  
  public class SingleLaneBridge {
  
      public static void main(String[] args) 
      {
          final Bridge bridge = new Bridge();
  
          Thread thNorthbound = new Thread( new Runnable() {
  
              @Override
              public void run() {
  
                  while(true)
                  {
                      Farmer farmer = new Farmer(bridge);
                      Thread th = new Thread(farmer);
                      farmer.setName("North Farmer : "+th.getId());
                      th.start();
                      try
                      {
                          TimeUnit.SECONDS.sleep((long)(Math.random()*10));
                      }
                      catch(InterruptedException iex)
                      {
                          iex.printStackTrace();
                      }
                  }
  
              }
          });
  
          Thread thSouthbound = new Thread( new Runnable() {
  
              @Override
              public void run() {
  
                  while(true)
                  {
                      Farmer farmer = new Farmer(bridge);
                      Thread th = new Thread(farmer);
                      farmer.setName("South Farmer : "+th.getId());
                      th.start();
                      try
                      {
                          TimeUnit.SECONDS.sleep((long)(Math.random()*10));
                      }
                      catch(InterruptedException iex)
                      {
                          iex.printStackTrace();
                      }
                  }
              }
          });
  
          thNorthbound.start();
          thSouthbound.start();
      }
  
  }
  
  class Bridge
  {
      private final Semaphore semaphore;
  
      public Bridge()
      {
          semaphore = new Semaphore(1);
      }
      public void crossBridge(Farmer farmer)
      {
          try
          {
              System.out.printf("Farmer %s is trying to cross the bridge.\n",farmer.getName());
              semaphore.acquire();
              System.out.printf("Farmer %s is crossing the bridge.\n",farmer.getName());
              long duration = (long)(Math.random() * 10);
              TimeUnit.SECONDS.sleep(duration);
          }
          catch(InterruptedException iex)
          {
              iex.printStackTrace();
          }
          finally
          {
              System.out.printf("Farmer %s has crossed the bridge.\n",farmer.getName());
              semaphore.release();
          }
      }
  }
  
  class Farmer implements Runnable
  {
      private String name;
      private Bridge bridge;
  
      public Farmer(Bridge bridge)
      {
          this.bridge = bridge;
      }
  
      public void run()
      {
          bridge.crossBridge(this);
      }
  
      public String getName() {
          return name;
      }
  
      public void setName(String name) {
          this.name = name;
      }
  
  }
  </code></pre>