2017年9月17日 | Leave a comment 问题: 解决: //ExchangerTest.java //javac ExchangerTest.java && java ExchangerTest import java.util.concurrent.*; class ExchangerTest{ public static void main(String[] argv){ Exchanger<String> ex = new Exchanger<>(); Thread t1 = new Thread(()->{ try{ String msg = ex.exchange("T1 welcome"); System.out.println("t1 get msg : "+msg); }catch(Exception e){} }); t1.start(); Thread t2 = new Thread(()->{ try{ Thread.sleep(1000); String msg = ex.exchange("T2 welcome"); System.out.println("t2 get msg : "+msg); }catch(Exception e){} }); t2.start(); } } 1234567891011121314151617181920212223242526 //ExchangerTest.java//javac ExchangerTest.java && java ExchangerTest import java.util.concurrent.*; class ExchangerTest{ public static void main(String[] argv){ Exchanger<String> ex = new Exchanger<>(); Thread t1 = new Thread(()->{ try{ String msg = ex.exchange("T1 welcome"); System.out.println("t1 get msg : "+msg); }catch(Exception e){} }); t1.start(); Thread t2 = new Thread(()->{ try{ Thread.sleep(1000); String msg = ex.exchange("T2 welcome"); System.out.println("t2 get msg : "+msg); }catch(Exception e){} }); t2.start(); }} 参考: