public class JoinDemo {
/**
* @param args
*/
public static void main(String[] args) {
System.out.println("進(jìn)入 main 方法...");
System.out.println("main 線程:" + Thread.currentThread().getName());
MyThread mt = new MyThread();
mt.setName("myThread-89757");
mt.start();
// try {
// mt.join();
// } catch (InterruptedException e) {
// e.printStackTrace();
// }
System.out.println("繼續(xù) main 方法");
}
}
class MyThread extends Thread {
@Override
public void run() {
System.out.println("線程:" + getName() +", 啟動(dòng).........");
try {
//模擬干非常耗時(shí)的活
Thread.currentThread().sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("線程:" + getName() +", 活干完啦!!!.........");
}
}
在粗體部分注釋時(shí)輸出結(jié)果為:
進(jìn)入 main 方法...
main 線程:main
繼續(xù) main 方法
線程:myThread-89757, 啟動(dòng).........
線程:myThread-89757, 活干完啦!!!.........
取消了粗體部分注釋后輸出結(jié)果為:
進(jìn)入 main 方法...
main 線程:main
線程:myThread-89757, 啟動(dòng).........
線程:myThread-89757, 活干完啦!!!.........
繼續(xù) main 方法
本站僅提供存儲(chǔ)服務(wù),所有內(nèi)容均由用戶發(fā)布,如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)
點(diǎn)擊舉報(bào)。