You are on page 1of 1

import java.util.concurrent.

Semaphore; public class SemaphoreTest { Semaphore binary = new Semaphore(1); public static void main(String args[]) { final SemaphoreTest test = new SemaphoreTest(); new Thread(){ @Override public void run(){ test.mutualExclusion(); } }.start(); new Thread(){ @Override public void run(){ test.mutualExclusion(); } }.start(); } private void mutualExclusion() { try { binary.acquire(); //mutual exclusive region System.out.println(Thread.currentThread().getName() + " inside mutual exclusive region"); Thread.sleep(1000); } catch (InterruptedException ie) { ie.printStackTrace(); } finally { binary.release(); System.out.println(Thread.currentThread().getName() + " outside of mutual exclusive region"); } } }

************OutPut********************* Thread-0 Thread-0 Thread-1 Thread-1 inside mutual exclusive region outside of mutual exclusive region inside mutual exclusive region outside of mutual exclusive region

You might also like