当我加入一个线程时,第一个线程需要先运行,然后第二个thrads运行是否正确?如果是这样,我如何加入Main方法中的两个线程?
public class SynchronizedThreads implements Runnable {
Thread t;
public SynchronizedThreads() {
t = new Thread(this, "Synched Thread");
t.start();
}
public void run() {
for(int i = 0; i < 100; i++) {
System.out.println(i);
try {
t.sleep(1000);
}
catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
public class StartSynchronizedThreads {
public static void main(String[] args) {
SynchronizedThreads x = new SynchronizedThreads();
SynchronizedThreads y = new SynchronizedThreads();
}
}
答案 0 :(得分:0)
try {
x.t.join();
y.t.join();
} catch (InterruptedException e) {
System.out.println("Main thread Interrupted");
}