# Java如何在兩個線程間進行通訊
## 引言
在多線程編程中,線程間的通信(Inter-Thread Communication)是核心問題之一。Java提供了多種機制實現線程間數據傳遞和協調,包括共享內存、等待/通知機制、阻塞隊列等。本文將深入探討這些方法及其適用場景。
---
## 一、共享內存(Shared Memory)
### 1.1 基本原理
通過共享對象或變量實現數據交換,需配合`synchronized`保證線程安全。
```java
class SharedObject {
private String message;
public synchronized void setMessage(String msg) {
this.message = msg;
}
public synchronized String getMessage() {
return message;
}
}
wait()
: 釋放鎖并進入等待狀態notify()
/notifyAll()
: 喚醒等待線程class MessageQueue {
private String message;
private boolean empty = true;
public synchronized void put(String msg) {
while (!empty) wait();
message = msg;
empty = false;
notifyAll();
}
public synchronized String take() {
while (empty) wait();
empty = true;
notifyAll();
return message;
}
}
notifyAll()
避免信號丟失BlockingQueue<String> queue = new ArrayBlockingQueue<>(10);
// 生產者線程
queue.put("Data");
// 消費者線程
String data = queue.take();
面向字節流/字符流的單向通信:
PipedInputStream pis = new PipedInputStream();
PipedOutputStream pos = new PipedOutputStream(pis);
// 線程1寫入
pos.write("Hello".getBytes());
// 線程2讀取
pis.read();
CountDownLatch latch = new CountDownLatch(2);
// 線程A/B
latch.countDown();
// 主線程
latch.await();
CyclicBarrier barrier = new CyclicBarrier(3);
// 所有線程到達屏障后繼續執行
barrier.await();
Exchanger<String> exchanger = new Exchanger<>();
// 線程A
String dataA = exchanger.exchange("DataA");
// 線程B
String dataB = exchanger.exchange("DataB");
class ProducerConsumer {
private final BlockingQueue<Integer> queue = new LinkedBlockingQueue<>(5);
class Producer implements Runnable {
public void run() {
try {
for (int i = 0; i < 10; i++) {
queue.put(i);
System.out.println("Produced: " + i);
}
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
}
}
class Consumer implements Runnable {
public void run() {
try {
while (true) {
Integer value = queue.take();
System.out.println("Consumed: " + value);
}
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
}
}
}
方法 | 適用場景 | 線程安全 | 復雜度 |
---|---|---|---|
共享內存 | 簡單數據交換 | 需同步 | 中 |
Wait/Notify | 精確控制執行順序 | 是 | 高 |
BlockingQueue | 生產者-消費者模式 | 是 | 低 |
管道 | 流式數據處理 | 是 | 中 |
并發工具類 | 復雜同步場景 | 是 | 中 |
java.util.concurrent
包中的工具類Thread.stop()
等廢棄方法InterruptedException
volatile
保證可見性Lock
和Condition
替代synchronized
Java線程通信的選擇取決于具體場景需求。對于簡單交互,共享變量或BlockingQueue可能足夠;復雜協調則需要Wait/Notify或并發工具類。理解每種機制的底層原理是寫出健壯多線程程序的關鍵。
提示:Java 21引入的虛擬線程(Virtual Threads)進一步簡化了并發編程模型,值得關注。 “`
(全文約1350字,可根據需要調整具體實現細節或擴展案例部分)
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。