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

offered
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);
    }
}
  1. 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;
  2. for (int i = 0; i < offered; i++)

    pass 1 of 2
    10for (int i0 = 0; i < offered2; i++) {11    if (queue.offer(i)) {
  3. accepted ← 1

    pass 1 of 2
    10for (int i = 0; i < offered; i++) {11    if (queue.offer(i0)) {12        accepted→ 1++;13    }
  4. for (int i = 0; i < offered; i++)

    pass 2 of 2
    10for (int i1 = 0; i < offered2; i++) {11    if (queue.offer(i)) {
  5. accepted ← 2

    pass 2 of 2
    10for (int i = 0; i < offered; i++) {11    if (queue.offer(i1)) {12        accepted→ 2++;13    }
  6. 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
  1. 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;
  2. for (int i = 0; i < offered; i++)

    10for (int i0 = 0; i < offered1; i++) {11    if (queue.offer(i)) {
  3. accepted ← 1

    10for (int i = 0; i < offered; i++) {11    if (queue.offer(i0)) {12        accepted→ 1++;13    }
  4. 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
  1. 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;
  2. for (int i = 0; i < offered; i++)

    pass 1 of 4
    10for (int i0 = 0; i < offered4; i++) {11    if (queue.offer(i)) {
    All 4 passes — pass 1 is the card above
    passi
    10
    21
    32
    43
  3. accepted ← 1

    pass 1 of 3
    10for (int i = 0; i < offered; i++) {11    if (queue.offer(i0)) {12        accepted→ 1++;13    }
    All 3 passes — pass 1 is the card above
    passiaccepted
    100 1
    211 2
    322 3
  4. 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