java 測時多執行緒執行時的疑問?
public class Cai implements Runnable {
@Override
public synchronized void run() {
for (int i = 0; i < 100; i++) {
System.out.println(Thread.currentThread().getName()+" : "+i);
}
}
}
public class Run {
@Test
public void test2() throws Exception {
Cai cai = new Cai();
Thread thread = new Thread(cai);
Thread thread2 = new Thread(cai);
Thread thread3 = new Thread(cai);
thread.setName("线程1");
thread2.setName("线程2");
thread3.setName("线程3");
thread.start();
thread2.start();
thread3.start();
}
}
當執行test2方法時,為什麼會出現如: 線程1循環0-99,線程2循環0-10然後程序就結束了,這是為什麼?
線程2沒有執行完全,線程3就沒有執行到???
加上 thread.join,主執行緒才會等待這個執行緒執行完畢
我的可以啊,每次都可以