LinkedBlockingDeque Two Ended Handoff
Feed urgent jobs from the head and routine jobs from the tail of a bounded LinkedBlockingDeque; timed offerLast degrades instead of blocking forever.
Code
import java.util.concurrent.LinkedBlockingDeque;
import java.util.concurrent.TimeUnit;
public class TwoEndedHandoff {
public static void main(String[] args) throws InterruptedException {
// 有界双端阻塞队列:容量 3,队首插紧急任务、队尾插常规任务
LinkedBlockingDeque<String> lane = new LinkedBlockingDeque<>(3);
lane.putLast("normal-1");
lane.putLast("normal-2");
lane.putFirst("urgent-1"); // [urgent-1, normal-1, normal-2] 已满
// 队列满:带超时的 offer 不无限阻塞,false 可走降级路径
boolean admitted = lane.offerLast("overflow-1", 50, TimeUnit.MILLISECONDS);
System.out.println("overflow admitted: " + admitted);
// 两端都可消费:takeLast 先取队尾,pollFirst 再取队首
System.out.println("takeLast -> " + lane.takeLast());
System.out.println("pollFirst -> " + lane.pollFirst());
System.out.println("pollFirst -> " + lane.pollFirst());
System.out.println("poll on empty -> " + lane.pollLast());
}
}How it works
Structurally, expect the class TwoEndedHandoff and nothing else.
Applying it
The demo output is labeled (overflow admitted:, takeLast ->, pollFirst ->), so every printed line maps back to its originating operation.
Statements that matter
boolean admitted = lane.offerLast("overflow-1", 50, TimeUnit.MILLISECONDS);
LinkedBlockingDeque<String> lane = new LinkedBlockingDeque<>(3);
lane.putLast("normal-1");
Interactive Tools for This Topic
FAQ
- What does LinkedBlockingDeque Two Ended Handoff print when run?
- Execute the whole file; each printed line is prefixed with a label like `overflow admitted:`, `takeLast ->`, `pollFirst ->`.
Report a problem with this exampleOpens a pre-filled issue on GitHub.