Semaphore In Java

April 03, 2017 by Sandeep Bhardwaj | Tags:


SemaphoreExample.java

import java.util.concurrent.Semaphore;

public class SemaphoreExample {

	public static void main(String[] args) {
		Semaphore semaphore = new Semaphore(2);

		new Thread(new Task(semaphore)).start();
		new Thread(new Task(semaphore)).start();
		new Thread(new Task(semaphore)).start();
		new Thread(new Task(semaphore)).start();
	}

}

class Task implements Runnable {

	Semaphore semaphore;

	public Task(Semaphore semaphore) {
		this.semaphore = semaphore;
	}

	@Override
	public void run() {
		try {
			semaphore.acquire();
			System.out.println(Thread.currentThread().getName() + " acquired");

			Thread.sleep(2000);
		} catch (InterruptedException e) {
			e.printStackTrace();
		} finally {
			semaphore.release();
			System.out.println(Thread.currentThread().getName() + " released");
		}
	}
}

Output

Thread-0 acquired
Thread-1 acquired
Thread-1 released
Thread-0 released
Thread-2 acquired
Thread-3 acquired
Thread-2 released
Thread-3 released