Concurrency Coordination Reports
Semaphore Capacity Coordination Report
Grant requests against a fixed permit pool and report the remaining capacity.
semaphore
A `Semaphore` hands out a bounded number of permits, so the permits still available describe how much shared capacity remains.
Semaphore Capacity Coordination Report
SemaphoreCapacityCoordinationReport.java
Replay: real traced execution (multi-file project)
import java.util.concurrent.Semaphore;
public class SemaphoreCapacityCoordinationReport {
public static void main(String[] args) {
int requests = 2;
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 = 0;
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 = 3;
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);
}
}
requests ← 2, granted ← 0
3public class SemaphoreCapacityCoordinationReport {4 public static void main(String[] args) {5 int requests→ 2 = 2; //@requests=0, 36 Semaphore permits = new Semaphore(3);7 int granted→ 0 = 0;for (int i = 0; i < requests; i++)
pass 1 of 29for (int i0 = 0; i < requests2; i++) {10 if (permits.tryAcquire()) {granted ← 1
pass 1 of 29for (int i = 0; i < requests; i++) {10 if (permits.tryAcquire()) {11 granted→ 1++;12 }for (int i = 0; i < requests; i++)
pass 2 of 29for (int i1 = 0; i < requests2; i++) {10 if (permits.tryAcquire()) {granted ← 2
pass 2 of 29for (int i = 0; i < requests; i++) {10 if (permits.tryAcquire()) {11 granted→ 2++;12 }available ← 1, status ← tight
15 int available→ 1 = permits.availablePermits();16 String status→ tight = available1 == 0 ? "full" : available <= 1 ? "tight" : "open";1718 System.out.println("requests=" + requests2 + " granted=" + granted2 + " available=" + available1 + " status=" + statustight);19}outputrequests=2 granted=2 available=1 status=tight
requests ← 0, granted ← 0, available ← 3, status ← open
3public class SemaphoreCapacityCoordinationReport {4 public static void main(String[] args) {5 int requests→ 0 = 0;6 Semaphore permits = new Semaphore(3);7 int granted→ 0 = 0;89 for (int i = 0; i < requests; i++) {10 if (permits.tryAcquire()) {11 granted++;12 }13 }1415 int available→ 3 = permits.availablePermits();16 String status→ open = available3 == 0 ? "full" : available <= 1 ? "tight" : "open";1718 System.out.println("requests=" + requests0 + " granted=" + granted0 + " available=" + available3 + " status=" + statusopen);19 }outputrequests=0 granted=0 available=3 status=open
requests ← 3, granted ← 0
3public class SemaphoreCapacityCoordinationReport {4 public static void main(String[] args) {5 int requests→ 3 = 3;6 Semaphore permits = new Semaphore(3);7 int granted→ 0 = 0;for (int i = 0; i < requests; i++)
pass 1 of 39for (int i0 = 0; i < requests3; i++) {10 if (permits.tryAcquire()) {All 3 passes — pass 1 is the card above pass i1 0 2 1 3 2 granted ← 1
pass 1 of 39for (int i = 0; i < requests; i++) {10 if (permits.tryAcquire()) {11 granted→ 1++;12 }All 3 passes — pass 1 is the card above pass granted1 0 → 1 2 1 → 2 3 2 → 3 available ← 0, status ← full
15 int available→ 0 = permits.availablePermits();16 String status→ full = available0 == 0 ? "full" : available <= 1 ? "tight" : "open";1718 System.out.println("requests=" + requests3 + " granted=" + granted3 + " available=" + available0 + " status=" + statusfull);19}outputrequests=3 granted=3 available=0 status=full