線程的狀態
線程的5種狀態:
http://blog.csdn.net/pange1991/article/details/53860651
http://www.cnblogs.com/xll1025/p/6415283.html
join()方法詳解:
http://blog.csdn.net/studyvcmfc/article/details/7426385
解釋一下,是主線程等待子線程的終止。也就是在子線程調用了join()方法後面的代碼,只有等到子線程結束了才能執行。
自從接觸Java多線程,一直對Join理解不了。JDK是這樣說的:join public final void join(long millis)throws InterruptedException Waits at most millis milliseconds for this thread to die. A timeout of 0 means to wait forever.大家能理解嗎? 字面意思是等待一段時間直到這個線程死亡,我的疑問是那個線程,是它本身的線程還是調用它的線程的,上代碼:
/**
*
* @author vma
*/
public class JoinTest {
public static void main(String[] args) {
Thread t = new Thread(new RunnableImpl());
t.start();
try {
t.join(1000);
System.out.println("joinFinish");
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
class RunnableImpl implements Runnable {
@Override
public void run() {
try {
System.out.println("Begin sleep");
Thread.sleep(1000);
System.out.println("End sleep");
e.printStackTrace();
}
}
}
結果是:
Begin sleep
End sleep
joinFinish
明白了吧,當main線程調用t.join時,main線程等待t線程,等待時間是1000,如果t線程Sleep 2000呢
public void run() {
try {
System.out.println("Begin sleep");
// Thread.sleep(1000);
Thread.sleep(2000);
System.out.println("End sleep");
} catch (InterruptedException e) {
e.printStackTrace();
}
}
結果是:
Begin sleep
joinFinish
End sleep
線程的狀態