多執行緒環境下,多個來源提交列印任務,打印機按順序/優先級處理,支援同步回調通知完成。
// 任務實體
class PrintTask {
final String docId;
final byte[] data;
final int priority; // 越大越優先
final Consumer callback;
PrintTask(String docId, byte[] data, int priority, Consumer cb) {
this.docId = docId; this.data = data; this.priority = priority; this.callback = cb;
}
}
// 佇列管理器 (單例 + 執行緒安全)
class PrintQueue {
private static final PrintQueue INSTANCE = new PrintQueue();
private final LinkedList queue = new LinkedList<>();
private final Object lock = new Object();
private volatile boolean running = false;
private PrintQueue() {}
public static PrintQueue getInstance() { return INSTANCE; }
// 普通提交 (尾部)
public void submit(PrintTask task) {
synchronized (lock) { queue.addLast(task); lock.notify(); }
}
// 優先/緊急提交 (頭部)
public void submitUrgent(PrintTask task) {
synchronized (lock) { queue.addFirst(task); lock.notify(); }
}
// 處理迴圈 (單獨執行緒啟動)
public void start(Consumer processor) {
synchronized (lock) {
if (running) return;
running = true;
}
new Thread(() -> {
while (true) {
PrintTask task;
synchronized (lock) {
while (queue.isEmpty()) {
try { lock.wait(); } catch (InterruptedException e) { return; }
}
task = queue.removeFirst();
}
processor.accept(task); // 實際打印邏輯
if (task.callback != null) task.callback.accept(task);
}
}, "Print-Worker").start();
}
public void shutdown() {
synchronized (lock) { running = false; lock.notifyAll(); }
}
}
public class PrintDemo {
public static void main(String[] args) {
PrintQueue queue = PrintQueue.getInstance();
// 啟動處理器 (模擬打印耗時)
queue.start(task -> {
System.out.println("打印中: " + task.docId);
try { Thread.sleep(500); } catch (InterruptedException ignored) {}
System.out.println("完成: " + task.docId);
});
// 提交任務
for (int i = 0; i < 5; i++) {
final int n = i;
queue.submit(new PrintTask("Doc-" + n, new byte[0], 0, t ->
System.out.println("回調: " + t.docId + " 完成")));
}
// 緊急插隊
queue.submitUrgent(new PrintTask("URGENT", new byte[0], 10, null));
// 等待演示結束
try { Thread.sleep(3000); } catch (InterruptedException ignored) {}
queue.shutdown();
}
}