Source Code : A semaphore

Java Is Open Source Programming Language You Can Download From Java and Java Libraries From http://www.oracle.com. Click Here to download
We provide this code related to title for you to solve your developing problem easily. Libraries which is import in this program you can download from http://www.oracle.com. Click Here or search from google with Libraries Name you get jar file related it

A semaphore

  
import java.util.concurrent.Semaphore;

public class Main {
  public static void main(String args[]) throws Exception {
    Semaphore sem = new Semaphore(1, true);
    Thread thrdA = new Thread(new SyncOutput(sem, "Message 1"));
    Thread thrdB = new Thread(new SyncOutput(sem, "Message 2!"));

    thrdA.start();
    thrdB.start();

    thrdA.join();
    thrdB.join();

  }
}

class SyncOutput implements Runnable {
  Semaphore sem;
  String msg;
  SyncOutput(Semaphore s, String m) {
    sem = s;
    msg = m;
  }

  public void run() {
    try {
      sem.acquire();
      System.out.println(msg);
      Thread.sleep(10);
    } catch (Exception exc) {
      System.out.println("Error Writing File");
    }
    sem.release();
  }
}

   
    
  

Thank with us