0
public static void main(String[] args) throws Exception {
final Exchanger<String> exchanger = new Exchanger<String>();
new Thread(new Runnable() {
@Override
public void run() {
try {
System.out.println(Thread.currentThread().getName() + exchanger.exchange("this came from subthread"));
} catch (InterruptedException ex) {
System.out.println("interrupted while waiting for message");
}
}
}).start();
System.out.println(Thread.currentThread().getName() + exchanger.exchange("this came from main thread"));
String s = exchanger.exchange("this came from main thread");
}
mainthis came from subthread
Thread-0this came from main thread
爲什麼在JVM永不放棄嗎?
如果主方法中的最後一行被刪除,該怎麼辦? – Subin
@SubinS也可以。 – assylias
當然,這是從我這樣一個愚蠢的問題..有一次,複製粘貼導致了這個錯誤...應該已經看到我的代碼在這裏..在日食,不知何故這兩行是並排粘貼,所以我完全錯過了.. :( – Achow