import java.util.concurrent.Exchanger;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class TestExchanger {
public static void main(String[] args) {
ExecutorService pool = Executors.newFixedThreadPool(2);
final Exchanger<String> exg = new Exchanger<String>();
pool.execute(new Runnable(){
@Override
public void run() {
System.out.println("this is Thread A");
String A = "i am A";
try {
String b = exg.exchange(A);
System.out.println(b);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
pool.execute(new Runnable(){
@Override
public void run() {
System.out.println("this is B");
String B = "i am B";
try {
String A = exg.exchange(B);
System.out.println(A);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
pool.shutdown();
}
}