class MyThread implements Runnable{ @Override public void run() { // TODO Auto-generated method stub for(int i=0;i<10;i++){ System.out.println(Thread.currentThread().getName()); } } }public class Test { public static void main(String[] args) { MyThread my = new MyThread(); Thread t = new Thread(my,"thread-a"); t.start(); for(int i=0;i<100;i++){ System.out.println(i); if(i>50){ try { t.join(); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } } }代碼如上, 我知道在i>50 會(huì)強(qiáng)制t運(yùn)行, 但是t.start()時(shí) 線程t 不就已經(jīng)啟動(dòng)了嗎?為什么在i<50時(shí)不會(huì)輸出thread-a?
1 回答

素胚勾勒不出你
TA貢獻(xiàn)1827條經(jīng)驗(yàn) 獲得超9個(gè)贊
你看到的只是你單次運(yùn)行的結(jié)果罷了(由于線程調(diào)度,可能使得 i > 50 之后才開始輸出 thread-a),你多運(yùn)行幾次就可以看到你想要的結(jié)果了:
添加回答
舉報(bào)
0/150
提交
取消