广元煌褂旅行社

當(dāng)前位置:

java認(rèn)證考試專業(yè)語(yǔ)言串講Java中join方法介紹與用法詳解

發(fā)表時(shí)間:2015/4/9 13:47:28 來(lái)源:互聯(lián)網(wǎng) 點(diǎn)擊關(guān)注微信:關(guān)注中大網(wǎng)校微信
關(guān)注公眾號(hào)

Java中join方法介紹與用法詳解

方法Join 是干啥用的? 簡(jiǎn)單回答,同步,如何同步? 怎么實(shí)現(xiàn)的? 下面將逐個(gè)回答。

自從接觸Java 多線程,一直對(duì)Join 理解不了。JDK 是這樣說(shuō)的: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. 大家能理解嗎? 字面意思是等待一段時(shí)間直到這個(gè)線程死亡,我的疑問(wèn)是那個(gè)線程,是它本身的線程還是調(diào)用它的線程的,上代碼:

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 秒,不管子線程什么時(shí)候結(jié)束

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();

}

}

}

結(jié)果是:

Begin sleep

End sleep

joinFinish

明白了吧, 當(dāng)main 線程調(diào)用t.join 時(shí),main 線程等待t 線程 ,等待時(shí)間是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();

}

}

結(jié)果是:

Begin sleep

joinFinish

End sleep

也就是說(shuō) main 線程只等1000 毫秒,不管T 什么時(shí)候結(jié)束 ,如果是t.join() 呢, 看代碼:

public final void join() throws InterruptedException {

join(0);

}

就是說(shuō)如果是t.join() = t.join(0)  JDK 這樣說(shuō)的 A timeout of 0 means to wait forever 字面意思是永遠(yuǎn)等待,是這樣嗎?

其實(shí)是等到t 結(jié)束后。

這個(gè)是怎么實(shí)現(xiàn)的嗎? 看JDK 代碼:

/**

* Waits at most millis milliseconds for this thread to

* die. A timeout of 0 means to wait forever.

*

* @param millis the time to wait in milliseconds.

* @exception InterruptedException if any thread has interrupted

* the current thread. The interrupted status 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;

}

}

}

其實(shí)Join 方法實(shí)現(xiàn)是通過(guò)wait (小提示:Object 提供的方法)。 當(dāng)main 線程調(diào)用t.join 時(shí)候,main 線程會(huì)獲得線程對(duì)象t 的鎖 (wait 意味著拿到該對(duì)象的鎖), 調(diào)用該對(duì)象的wait( 等待時(shí)間) ,直到該對(duì)象喚醒main 線程,比如退出后。

這就意味著main 線程調(diào)用t.join 時(shí),必須能夠拿到線程t 對(duì)象的鎖 ,如果拿不到它是無(wú)法wait 的,剛開(kāi)的例子t.join(1000) 不是說(shuō)明了main 線程等待1 秒,如果在它等待之前,其他線程獲取了t 對(duì)象的鎖,它等待時(shí)間可不就是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();// 這個(gè)線程會(huì)持有鎖

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 方法中 通過(guò)new ThreadTest(t).start(); 實(shí)例化ThreadTest 線程對(duì)象, 它在 holdThreadLock() 方法中,通過(guò) synchronized (thread) ,獲取線程對(duì)象t 的鎖,并Sleep (9000 )后釋放,這就意味著,即使

main 方法t.join(1000), 等待一秒鐘,它必須等待 ThreadTest 線程釋放t 鎖后才能進(jìn)入wait 方法中,它實(shí)際等待時(shí)間是9000+1000 MS

運(yùn)行結(jié)果是:

getObjectLock

Begin sleep

End sleep

ReleaseObjectLock

joinFinish

編輯推薦

java認(rèn)證考試專業(yè)語(yǔ)言串講雙緩沖原理在實(shí)現(xiàn)消除閃爍

java認(rèn)證考試專業(yè)語(yǔ)言串講struts2文件上傳的三種方式

(責(zé)任編輯:xy)

2頁(yè),當(dāng)前第1頁(yè)  第一頁(yè)  前一頁(yè)  下一頁(yè)
最近更新 考試動(dòng)態(tài) 更多>
广灵县| 措美县| 丁青县| 英山县| 肃宁县| 石林| 潢川县| 民县| 深圳市| 抚顺市| 平凉市| 巨鹿县| 浏阳市| 团风县| 五指山市| 东明县| 漳浦县| 绥中县| 余姚市| 汾阳市| 松溪县| 延吉市| 家居| 芦山县| 山西省| 垫江县| 丰原市| 永济市| 娱乐| 江阴市| 宽甸| 瑞昌市| 巴中市| 潍坊市| 进贤县| 巴马| 西平县| 大同市| 科技| 边坝县| 玉树县|