- A semaphore controls access to the shared resource through the use of a counter.
- if the counter is greater than 0, then access is allowed. if it is zero, then access is denied.
- what the counter is counting are permits that allow access to the shared resource. to access the resource, a thread must be granted a permit from the semaphore.
- In the below example if you increase the permit of the semaphore, then multiple threads will access the counter and the operations will get intermixed.
import java.util.concurrent.Semaphore;
class Counter {
static int counter;
}
class CounterInc implements Runnable {
Semaphore sem;
CounterInc(Semaphore sm) {
this.sem = sm;
}
/**
* Runs this operation.
*/
@Override
public void run() {
String name = Thread.currentThread().getName();
try {
this.sem.acquire();
} catch (InterruptedException e) {
System.out.println(name + ">> interrupted");
}
try {
Thread.sleep(100);
} catch (InterruptedException ex) {
System.out.println("thread interrupted");
}
Counter.counter++;
this.sem.release();
}
}
class CounterDecr implements Runnable {
Semaphore sem;
CounterDecr(Semaphore sm) {
this.sem = sm;
}
/**
* Runs this operation.
*/
@Override
public void run() {
String name = Thread.currentThread().getName();
try {
this.sem.acquire();
} catch (InterruptedException e) {
System.out.println(name + ">> interrupted");
}
try {
Thread.sleep(10);
} catch (InterruptedException ex) {
System.out.println("thread interrupted");
}
Counter.counter--;
this.sem.release();
}
}
public class Main {
public static void main(String[] args) {
Semaphore sm = new Semaphore(1); // allow only 1 threads to access resource concurrently
System.out.println("Counter at start: " + Counter.counter);
Thread t1 = new Thread(new CounterInc(sm), "thread1");
Thread t2 = new Thread(new CounterInc(sm), "thread2");
Thread t3 = new Thread(new CounterDecr(sm), "thread3");
t1.start();
t2.start();
t3.start();
try {
t1.join();
t2.join();
t3.join();
} catch (InterruptedException e) {
System.out.println("Exception occurred");
}
System.out.println("Counter at end: " + Counter.counter); // 1
}
}