Concurrency Coordination Reports
Blocking Queue Coordination Report
Offer items into a bounded queue and report how many were buffered or dropped.
blocking queue
An `ArrayBlockingQueue` has a fixed capacity, so `offer` reports whether an item was buffered and the remaining capacity coordinates producers and consumers.
Blocking Queue Coordination Report
BlockingQueueCoordinationReport.java
Replay: real traced execution (multi-file project)
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;
public class BlockingQueueCoordinationReport {
public static void main(String[] args) {
int offered = 2;
BlockingQueue<Integer> queue = new ArrayBlockingQueue<>(3);
int accepted = 0;
for (int i = 0; i < offered; i++) {
if (queue.offer(i)) {
accepted++;
}
}
int remaining = queue.remainingCapacity();
String status = remaining == 0 ? "full" : accepted < offered ? "dropped" : remaining <= 1 ? "tight" : "open";
System.out.println("offered=" + offered + " accepted=" + accepted + " remaining=" + remaining + " status=" + status);
}
}
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;
public class BlockingQueueCoordinationReport {
public static void main(String[] args) {
int offered = 1;
BlockingQueue<Integer> queue = new ArrayBlockingQueue<>(3);
int accepted = 0;
for (int i = 0; i < offered; i++) {
if (queue.offer(i)) {
accepted++;
}
}
int remaining = queue.remainingCapacity();
String status = remaining == 0 ? "full" : accepted < offered ? "dropped" : remaining <= 1 ? "tight" : "open";
System.out.println("offered=" + offered + " accepted=" + accepted + " remaining=" + remaining + " status=" + status);
}
}
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;
public class BlockingQueueCoordinationReport {
public static void main(String[] args) {
int offered = 4;
BlockingQueue<Integer> queue = new ArrayBlockingQueue<>(3);
int accepted = 0;
for (int i = 0; i < offered; i++) {
if (queue.offer(i)) {
accepted++;
}
}
int remaining = queue.remainingCapacity();
String status = remaining == 0 ? "full" : accepted < offered ? "dropped" : remaining <= 1 ? "tight" : "open";
System.out.println("offered=" + offered + " accepted=" + accepted + " remaining=" + remaining + " status=" + status);
}
}
offered ← 2, queue ← [], accepted ← 0
4public class BlockingQueueCoordinationReport {5 public static void main(String[] args) {6 int offered→ 2 = 2; //@offered=1, 47 BlockingQueue<Integer> queue→ [] = new ArrayBlockingQueue<>(3);8 int accepted→ 0 = 0;for (int i = 0; i < offered; i++)
pass 1 of 210for (int i0 = 0; i < offered2; i++) {11 if (queue.offer(i)) {accepted ← 1
pass 1 of 210for (int i = 0; i < offered; i++) {11 if (queue.offer(i0)) {12 accepted→ 1++;13 }for (int i = 0; i < offered; i++)
pass 2 of 210for (int i1 = 0; i < offered2; i++) {11 if (queue.offer(i)) {accepted ← 2
pass 2 of 210for (int i = 0; i < offered; i++) {11 if (queue.offer(i1)) {12 accepted→ 2++;13 }remaining ← 1, status ← tight
16 int remaining→ 1 = queue.remainingCapacity();17 String status→ tight = remaining1 == 0 ? "full" : accepted2 < offered2 ? "dropped" : remaining <= 1 ? "tight" : "open";1819 System.out.println("offered=" + offered2 + " accepted=" + accepted2 + " remaining=" + remaining1 + " status=" + statustight);20}outputoffered=2 accepted=2 remaining=1 status=tight
offered ← 1, queue ← [], accepted ← 0
4public class BlockingQueueCoordinationReport {5 public static void main(String[] args) {6 int offered→ 1 = 1;7 BlockingQueue<Integer> queue→ [] = new ArrayBlockingQueue<>(3);8 int accepted→ 0 = 0;for (int i = 0; i < offered; i++)
10for (int i0 = 0; i < offered1; i++) {11 if (queue.offer(i)) {accepted ← 1
10for (int i = 0; i < offered; i++) {11 if (queue.offer(i0)) {12 accepted→ 1++;13 }remaining ← 2, status ← open
16 int remaining→ 2 = queue.remainingCapacity();17 String status→ open = remaining2 == 0 ? "full" : accepted1 < offered1 ? "dropped" : remaining <= 1 ? "tight" : "open";1819 System.out.println("offered=" + offered1 + " accepted=" + accepted1 + " remaining=" + remaining2 + " status=" + statusopen);20}outputoffered=1 accepted=1 remaining=2 status=open
offered ← 4, queue ← [], accepted ← 0
4public class BlockingQueueCoordinationReport {5 public static void main(String[] args) {6 int offered→ 4 = 4;7 BlockingQueue<Integer> queue→ [] = new ArrayBlockingQueue<>(3);8 int accepted→ 0 = 0;for (int i = 0; i < offered; i++)
pass 1 of 410for (int i0 = 0; i < offered4; i++) {11 if (queue.offer(i)) {All 4 passes — pass 1 is the card above pass i1 0 2 1 3 2 4 3 accepted ← 1
pass 1 of 310for (int i = 0; i < offered; i++) {11 if (queue.offer(i0)) {12 accepted→ 1++;13 }All 3 passes — pass 1 is the card above pass iaccepted1 0 0 → 1 2 1 1 → 2 3 2 2 → 3 remaining ← 0, status ← full
16 int remaining→ 0 = queue.remainingCapacity();17 String status→ full = remaining0 == 0 ? "full" : accepted3 < offered4 ? "dropped" : remaining <= 1 ? "tight" : "open";1819 System.out.println("offered=" + offered4 + " accepted=" + accepted3 + " remaining=" + remaining0 + " status=" + statusfull);20}outputoffered=4 accepted=3 remaining=0 status=full