簡介
- 用於兩個工作線程之間交換數據的封裝工具類
- 簡單說就是一個線程在完成一定的事務後想與另一個線程交換數據,則第一個先拿出數據的線程會一直等待第二個線程,直到第二個線程拿着數據到來時才能彼此交換對應數據
Exchanger<V>
泛型類型,其中 V 表示可交換的數據類型
簡單的應用
public class ExchangerTest {
public static void main(String[] args) {
final Exchanger<String> exchanger = new Exchanger<>();
new Thread(()->{
System.out.println(Thread.currentThread().getName() + " start . ");
try {
String exchange = exchanger.exchange("I am come from T-A");
System.out.println(Thread.currentThread().getName() + " get value : " + exchange);
System.out.println(Thread.currentThread().getName() + " end . ");
} catch (InterruptedException e) {
e.printStackTrace();
}
},"A").start();
new Thread(()->{
System.out.println(Thread.currentThread().getName() + " start . ");
try {
String exchange = exchanger.exchange("I am come from T-B");
System.out.println(Thread.currentThread().getName() + " get value : " + exchange);
System.out.println(Thread.currentThread().getName() + " end . ");
} catch (InterruptedException e) {
e.printStackTrace();
}
},"B").start();
}
}
結果:
A start .
B start .
B get value : I am come from T-A
A get value : I am come from T-B
A end .
B end .
可以看出最重要的方法是exchange
重要方法介紹
public V exchange(V x) throws InterruptedException
- 等待另一個線程到達此交換點,然後將給定對象傳輸給它,接收其對象作爲回報。
- 可以被打斷
- 如果已經有個線程正在等待了,則直接交換數據
發送的數據的分析
public class ExchangerTest {
public static void main(String[] args) {
Object o = new Object();
final Exchanger<Object> exchanger = new Exchanger<>();
new Thread(()->{
try {
Object exchange = exchanger.exchange(null);
System.out.println("接收的" + o );
} catch (InterruptedException e) {
e.printStackTrace();
}
},"A").start();
new Thread(()->{
try {
System.out.println("發送的" + o);
exchanger.exchange(o);
} catch (InterruptedException e) {
e.printStackTrace();
}
},"B").start();
}
}
結果
發送的java.lang.Object@67deb479
接收的java.lang.Object@67deb479
從這個例子可以看出一個很嚴重的問題:發送的對象和接收的對象是同一個對象,可能會用嚴重的線程安全問題