1 java multithreading synchronization
我有两个线程,我希望第二个线程等到第一个线程完成.我怎么能做到这一点?
这是我的代码:
public class NewClass1 implements Runnable {
// in main
CallMatlab c = new CallMatlab();
CUI m = new CUI();
Thread t1 = new Thread(c);
t1.start();
Thread t2 = new Thread(m);
try {
t1.join();
} catch (InterruptedException ex) {
Logger.getLogger(NewClass1.class.getName()).log(Level.SEVERE, null, ex);
}
t2.start();
//
public void run() {
throw new UnsupportedOperationException("Not su..");
}
}
Run Code Online (Sandbox Code Playgroud)
使用该Thread.join()方法.从第二个帖子,打电话
firstThread.join();
Run Code Online (Sandbox Code Playgroud)
还有可选的重载也需要超时.如果InterruptedException第二个线程在第一个线程完成之前被中断,您将需要处理.