Concurrency Coordination Reports
Blocking Queue Coordination Report
Offer items into a bounded queue and report how many were buffered or dropped.
Blocking Queue Coordination Report
BlockingQueueCoordinationReport.java
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;
public class BlockingQueueCoordinationReport {
public static void main(String[] args) {
int offered = ;
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 = ;
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 = ;
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);
}
}
blocking queue
An `ArrayBlockingQueue` has a fixed capacity, so `offer` reports whether an item was buffered and the remaining capacity coordinates producers and consumers.