publicclass Run {
publicstaticvoidmain(String[] args) {
Exchanger<String> exchanger = new Exchanger<>();
Car car = new Car(exchanger);
Bike bike = new Bike(exchanger);
car.start();
bike.start();
System.out.println("Main end!");
}
}
Exchanger类源于java.util.concurrent包,它可以在两个线程之间传输数据,Exchanger中的public V exchange(V x)方法被调用后等待另一个线程到达交换点(如果当前线程没有被中断),然后将已知的对象传给它,返回接收的对象。如果另外一个线程已经在交换点等待,那么恢复线程计划并接收通过当前线程传给的对象:public class Car ext