Concurrency Coordination Reports
Semaphore Capacity Coordination Report
Grant requests against a fixed permit pool and report the remaining capacity.
Semaphore Capacity Coordination Report
SemaphoreCapacityCoordinationReport.java
import java.util.concurrent.Semaphore;
public class SemaphoreCapacityCoordinationReport {
public static void main(String[] args) {
int requests = ;
Semaphore permits = new Semaphore(3);
int granted = 0;
for (int i = 0; i < requests; i++) {
if (permits.tryAcquire()) {
granted++;
}
}
int available = permits.availablePermits();
String status = available == 0 ? "full" : available <= 1 ? "tight" : "open";
System.out.println("requests=" + requests + " granted=" + granted + " available=" + available + " status=" + status);
}
}
import java.util.concurrent.Semaphore;
public class SemaphoreCapacityCoordinationReport {
public static void main(String[] args) {
int requests = ;
Semaphore permits = new Semaphore(3);
int granted = 0;
for (int i = 0; i < requests; i++) {
if (permits.tryAcquire()) {
granted++;
}
}
int available = permits.availablePermits();
String status = available == 0 ? "full" : available <= 1 ? "tight" : "open";
System.out.println("requests=" + requests + " granted=" + granted + " available=" + available + " status=" + status);
}
}
import java.util.concurrent.Semaphore;
public class SemaphoreCapacityCoordinationReport {
public static void main(String[] args) {
int requests = ;
Semaphore permits = new Semaphore(3);
int granted = 0;
for (int i = 0; i < requests; i++) {
if (permits.tryAcquire()) {
granted++;
}
}
int available = permits.availablePermits();
String status = available == 0 ? "full" : available <= 1 ? "tight" : "open";
System.out.println("requests=" + requests + " granted=" + granted + " available=" + available + " status=" + status);
}
}
semaphore
A `Semaphore` hands out a bounded number of permits, so the permits still available describe how much shared capacity remains.