**截圖線程(生產者)** → **阻塞隊列** → **發送線程(消費者)**。
BlockingQueue queue = new LinkedBlockingQueue<>(10); // 緩衝 10 幀
// 截圖線程
new Thread(() -> {
while (running) {
Bitmap bmp = captureScreen(); // 需權限/媒體投影
try { queue.put(bmp); } catch (InterruptedException e) { break; }
Thread.sleep(captureIntervalMs);
}
}).start();
// 發送線程
new Thread(() -> {
while (running) {
try {
Bitmap bmp = queue.take();
sendViaSocket(bmp);
bmp.recycle(); // 及時回收內存
} catch (InterruptedException | IOException e) { break; }
Thread.sleep(sendIntervalMs);
}
}).start();
生產者-消費者 + 阻塞隊列是雙線程協作經典模式,解耦生產消費速度差異,配合動態節流實現流暢截圖推流。