简介
Exchanger(交换者),一个用于线程间协作的工具类。Exchanger用于线程间的数据交换,提供了一个同步点,两个线程可以在这个同步点交换彼此的数据。
构造方法
Exchanger()
所有方法
exchange(V x)
等待另一个线程到达这个交换点(除非当前线程interrupted),然后与另一线程交换数据对象。
exchange(V x, long timeout, TimeUnit unit)
等待另一个线程到达这个交换点(除非当前线程interrupted或超过指定的等待时间),然后与另一线程交换数据对象。
简单示例
有个羊集,就是各家农户都拉着自家的羊来卖,或者来买几只合适的羊。卖羊的和买羊的就可以看做是两个线程,买羊的手里拿着钱,卖羊的手里牵着羊,这笔买卖交易成功后,买羊的手里就牵着羊,卖羊的手里就拿着钱了。
import java.util.concurrent.Exchanger;
/**
* Created by bxw on 2017/12/3.
*/
public class SheepMarket {
Exchanger<String> exchanger = new Exchanger<>();
public static void main(String[] args) {
SheepMarket sheepMarket = new SheepMarket();
System.out.println("**************开市了*************");
SheepMarket.BuySheep buySheep = sheepMarket.new BuySheep();
SheepMarket.SaleSheep saleSheep = sheepMarket.new SaleSheep();
Thread thread1 = new Thread(buySheep);
Thread thread2 = new Thread(saleSheep);
thread1.start();
thread2.start();
}
public class BuySheep implements Runnable{
String person = "买羊的说: ";
String money = "我有1000块钱";
@Override
public void run() {
System.out.println(person + "我要买羊," + money);
try {
String sheep = exchanger.exchange(money);
System.out.println(person + "羊买完了," + sheep);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public class SaleSheep implements Runnable{
String person = "卖羊的说: ";
String sheep = "我手里牵着羊";
@Override
public void run() {
System.out.println(person + "我要卖羊," + sheep);
try {
String money = exchanger.exchange(sheep);
System.out.println(person + "羊卖完了," + money);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
上面的这个例子使用了exchange(V x)方法,下面再来看看exchange
(V x, long timeout, TimeUnit unit)这个方法,买羊的要买羊,卖羊的如果长时间不理买羊的,买羊的就走了。
将BuySheep和SaleSheep稍作修改:
public class SheepMarket {
Exchanger<String> exchanger = new Exchanger<>();
public static void main(String[] args) {
SheepMarket sheepMarket = new SheepMarket();
System.out.println("**************开市了*************");
SheepMarket.BuySheep buySheep = sheepMarket.new BuySheep();
SheepMarket.SaleSheep saleSheep = sheepMarket.new SaleSheep();
Thread thread1 = new Thread(buySheep);
Thread thread2 = new Thread(saleSheep);
thread1.start();
thread2.start();
}
public class BuySheep implements Runnable{
String person = "买羊的说: ";
String money = "我有1000块钱";
@Override
public void run() {
System.out.println(person + "我要买羊," + money);
try {
String sheep = exchanger.exchange(money, 8000, TimeUnit.MILLISECONDS);
System.out.println(person + "羊买完了," + sheep);
} catch (InterruptedException e) {
e.printStackTrace();
} catch (TimeoutException e) {
e.printStackTrace();
System.err.println("麻蛋,不理我,老子还不买了");
}
}
}
public class SaleSheep implements Runnable{
String person = "卖羊的说: ";
String sheep = "我手里牵着羊";
@Override
public void run() {
System.out.println(person + "我要卖羊," + sheep);
try {
Thread.sleep(9000);
String money = exchanger.exchange(sheep);
System.out.println(person + "羊卖完了," + money);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
把SaleSheep中Thread.sleep(9000)的睡眠时间缩短至8000以下依旧可以成功交换数据对象。
总结
如果两个线程有一个没有执行exchange(V x)方法,就会一直等待,如果担心有特殊情况发生,避免一直傻等,可以使用exchange(V x, long timeout, TimeUnit unit)。Exchanger还可用于校对工作,比如要对一批数据进行录入到excel,为了避免错误,可有甲乙二人进行录入,然后由系统加载这两个Excel,并比较是否录入一致。