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永远不会在这里退出?