Skip to content
AITroveRead. Build. Understand.
Make this comfortable

Java LinkedBlockingDeque: bound urgent and normal work in one queue

Last updated: 5 Oct 20265 min read
tutorial
AdvancedBy AITrove Editorial

LinkedBlockingDeque can enforce a fixed capacity while admitting work at either end. offerFirst and offerLast report a full queue without blocking.

Operational contract

The inbox uses 47 slots. Urgent receipts enter at the front, normal receipts at the back, and workers take from the front. Both admission paths share the same capacity. Repeated urgent arrivals can starve older normal items, so a production scheduler may need fairness or separate reserved capacity. The dequeue operation is interruptible. The wrapper owns the queue and does not expose mutating methods that could circumvent admission policy.

Failure case

There are 47 waiting receipts. Both urgent and normal submission return false rather than growing memory. When a worker takes one, exactly one slot opens for either class.

Java code

Java
import java.util.Objects;
import java.util.concurrent.LinkedBlockingDeque;

public class ReceiptWorkDeque {
    private final LinkedBlockingDeque<String> waiting = new LinkedBlockingDeque<>(47);

    public boolean submitUrgent(String receiptId) {
        return waiting.offerFirst(Objects.requireNonNull(receiptId));
    }

    public boolean submitNormal(String receiptId) {
        return waiting.offerLast(Objects.requireNonNull(receiptId));
    }

    public String take() throws InterruptedException {
        return waiting.takeFirst();
    }
}

Performance and ownership cost

End insertion and removal are O(1) queue work aside from lock contention or blocking, with O(N) node storage capped at 47. The cap covers waiting items; worker execution and upstream request retention need separate limits.

Common Mistakes

  • Do not use the no-argument constructor when a real queue cap is required.
  • Do not ignore false from offerFirst or offerLast.
  • Do not assume urgent-first ordering guarantees fair service for normal work.

Connected lessons

java
concurrent collections
linkedblockingdeque-urgent-admission
Storage details