歡迎您光臨本站 註冊首頁

Java中join方法介紹與用法

←手機掃碼閱讀     火星人 @ 2014-03-09 , reply:0
方法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. 大家能理解嗎? 字面意思是等待一段時間直到這個線程死亡,我的疑問是那個線程,是它本身的線程還是調用它的線程的,上代碼:
package concurrentstudy;
/**
*
* @author vma
*/
public class JoinTest {
public static void main(String[] args) {
Thread t = new Thread( new RunnableImpl());
t.start();
try {
t.join(1000) ; // 主線程只等1 秒,不管子線程什麼時候結束
System.out.println("joinFinish");
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
class RunnableImpl implements Runnable {
@Override
public void run() {
try {
System.out.println("Begin sleep");
Thread.sleep(1000);
System.out.println("End sleep");
} catch (InterruptedException e) {
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
也就是說 main 線程只等1000 毫秒,不管T 什麼時候結束 ,如果是t.join() 呢, 看代碼:
public final void join() throws InterruptedException {
join(0);
}
就是說如果是t.join() = t.join(0)  JDK 這樣說的 A timeout of 0 means to wait forever 字面意思是永遠等待,是這樣嗎?
其實是等到t 結束后.
這個是怎麼實現的嗎? 看JDK 代碼:
/**
* Waits at most <code>millis</code> milliseconds for this thread to
* die. A timeout of <code>0</code> means to wait forever.
*
* @param millis the time to wait in milliseconds.
* @exception InterruptedException if any thread has interrupted
* the current thread. The <i>interrupted status</i> of the
* current thread is cleared when this exception is thrown.
*/
public final synchronized void join( long millis)


throws InterruptedException {
long base = System.currentTimeMillis();
long now = 0;
if (millis < 0) {
throw new IllegalArgumentException("timeout value is negative");
}
if (millis == 0) {
while (isAlive()) {
wait(0);
}
} else {
while (isAlive()) {
long delay = millis - now;
if (delay <= 0) {
break ;
}
wait(delay);
now = System.currentTimeMillis() - base;
}
}
}
其實Join 方法實現是通過wait (小提示:Object 提供的方法). 當main 線程調用t.join 時候,main 線程會獲得線程對象t 的鎖 (wait 意味著拿到該對象的鎖), 調用該對象的wait( 等待時間) ,直到該對象喚醒main 線程,比如退出后.
這就意味著main 線程調用t.join 時,必須能夠拿到線程t 對象的鎖 ,如果拿不到它是無法wait 的,剛開的例子t.join(1000) 不是說明了main 線程等待1 秒,如果在它等待之前,其他線程獲取了t 對象的鎖,它等待時間可不就是1 毫秒了.上代碼介紹:
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package concurrentstudy;
/**
*
* @author vma
*/
public class JoinTest {
public static void main(String[] args) {
Thread t = new Thread( new RunnableImpl());
new ThreadTest(t).start();// 這個線程會持有鎖
t.start();
try {
t.join();
System.out.println("joinFinish");
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
class ThreadTest extends Thread {
Thread thread;
public ThreadTest(Thread thread) {
this .thread = thread;
}
@Override
public void run() {
holdThreadLock();
}
public void holdThreadLock() {
synchronized (thread) {
System.out.println("getObjectLock");
try {
Thread.sleep(9000);
} catch (InterruptedException ex) {
ex.printStackTrace();
}
System.out.println("ReleaseObjectLock");
}
}
}
class RunnableImpl implements Runnable {
@Override
public void run() {
try {
System.out.println("Begin sleep");
Thread.sleep(2000);
System.out.println("End sleep");
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
在main 方法中 通過new ThreadTest(t).start(); 實例化ThreadTest 線程對象, 它在 holdThreadLock() 方法中,通過 synchronized (thread) ,獲取線程對象t 的鎖,並Sleep (9000 )后釋放,這就意味著,即使
main 方法t.join(1000), 等待一秒鐘,它必須等待 ThreadTest 線程釋放t 鎖后才能進入wait 方法中,它實際等待時間是9000 1000 MS


運行結果是:
getObjectLock
Begin sleep
End sleep
ReleaseObjectLock
joinFinish


[火星人 ] Java中join方法介紹與用法已經有1656次圍觀

http://coctec.com/docs/java/show-post-60032.html