问题:
解决:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
//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(); } } |
参考: