本篇內(nèi)容介紹了“java線程join方法的使用方法”的有關(guān)知識,在實際案例的操作過程中,不少人都會遇到這樣的困境,接下來就讓小編帶領(lǐng)大家學(xué)習(xí)一下如何處理這些情況吧!希望大家仔細(xì)閱讀,能夠?qū)W有所成!
創(chuàng)新互聯(lián)服務(wù)項目包括襄城網(wǎng)站建設(shè)、襄城網(wǎng)站制作、襄城網(wǎng)頁制作以及襄城網(wǎng)絡(luò)營銷策劃等。多年來,我們專注于互聯(lián)網(wǎng)行業(yè),利用自身積累的技術(shù)優(yōu)勢、行業(yè)經(jīng)驗、深度合作伙伴關(guān)系等,向廣大中小型企業(yè)、政府機構(gòu)等提供互聯(lián)網(wǎng)行業(yè)的解決方案,襄城網(wǎng)站推廣取得了明顯的社會效益與經(jīng)濟效益。目前,我們服務(wù)的客戶以成都為中心已經(jīng)輻射到襄城省份的部分城市,未來相信會繼續(xù)擴大服務(wù)區(qū)域并繼續(xù)獲得客戶的支持與信任!
如下所示代碼,是并行執(zhí)行的
public class ThreadTest { //private static final Long count = 10000L; public static void main(String[] args){ long base = System.currentTimeMillis(); try { ThreadJoin t1 = new ThreadJoin("線程1"); ThreadJoin t2 = new ThreadJoin("線程2"); //t1.join(); t1.start(); t1.join(); t2.start(); } catch (Exception e) { e.printStackTrace(); } long time = System.currentTimeMillis() - base; System.out.println("執(zhí)行時間:"+time); } }class ThreadJoin extends Thread{ private static final Long count = 10L; public ThreadJoin(String name){ super(name); } @Override public void run() { //super.run(); for(int i = 1; i <= count; i ++){ System.out.println(this.getName()+":"+i); } }}
打印出來的信息,都是這樣的
執(zhí)行時間:0線程1:1線程2:1線程2:2線程2:3線程2:4線程2:5線程2:6線程2:7線程2:8線程2:9線程2:10線程1:2線程1:3線程1:4線程1:5線程1:6線程1:7線程1:8線程1:9線程1:10
要實現(xiàn)串行執(zhí)行,可以加上join方法,實現(xiàn)線程1執(zhí)行完成后才開始執(zhí)行線程2,也就是串行執(zhí)行
public class ThreadTest { //private static final Long count = 10000L; public static void main(String[] args){ long base = System.currentTimeMillis(); try { ThreadJoin t1 = new ThreadJoin("線程1"); ThreadJoin t2 = new ThreadJoin("線程2"); //t1.join(); t1.start(); t1.join(); t2.start(); } catch (Exception e) { e.printStackTrace(); } long time = System.currentTimeMillis() - base; System.out.println("執(zhí)行時間:"+time); } }class ThreadJoin extends Thread{ private static final Long count = 10L; public ThreadJoin(String name){ super(name); } @Override public void run() { //super.run(); for(int i = 1; i <= count; i ++){ System.out.println(this.getName()+":"+i); } }}
線程1:1線程1:2線程1:3線程1:4線程1:5線程1:6線程1:7線程1:8線程1:9線程1:10執(zhí)行時間:0線程2:1線程2:2線程2:3線程2:4線程2:5線程2:6線程2:7線程2:8線程2:9線程2:10
從執(zhí)行結(jié)果看,已經(jīng)是串行執(zhí)行線程
所以上面的例子是調(diào)了現(xiàn)場1的join方法,也就是說要先執(zhí)行完成線程1,然后才執(zhí)行main主線程
join方法的作用是,舉個例子,在A線程里調(diào)B線程的join方法時,要先B線程執(zhí)行完成,然后才會繼續(xù)執(zhí)行A線程
ok,上面調(diào)join方法是不加參數(shù)的,也可以加上參數(shù),比如線程A.join(10);,就是說線程A執(zhí)行10s后,繼續(xù)執(zhí)行B線程
注意:join時間參數(shù)缺省的情況,默認(rèn)是0,也就是說join()等同于join(0);
/** * Waits for this thread to die. * * <p> An invocation of this method behaves in exactly the same * way as the invocation * * <blockquote> * {@linkplain #join(long) join}{@code (0)} * </blockquote> * * @throws 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 void join() throws InterruptedException { join(0); }
Thread類里的源碼,可以看出默認(rèn)賦值為0,然后這個0是什么意思?0不是表示執(zhí)行0s,而是表示要A線程執(zhí)行完成才繼續(xù)執(zhí)行B線程的意思
ok,然后為什么調(diào)用了join方法就可以實現(xiàn)線程同步?我們簡單看一下代碼:
public final synchronized void join(long millis) throws InterruptedException { long base = System.currentTimeMillis(); long now = 0; //執(zhí)行時間必須為正數(shù) if (millis < 0) { throw new IllegalArgumentException("timeout value is negative"); } //執(zhí)行時間為0或者缺省情況 if (millis == 0) { while (isAlive()) {//表示線程還沒執(zhí)行好 wait(0);//調(diào)用線程的wait方法 } } else {//執(zhí)行時間大于0的情況 while (isAlive()) { long delay = millis - now;//循環(huán)計算延期時間 if (delay <= 0) { break; } wait(delay);//同樣調(diào)用線程的wait方法 now = System.currentTimeMillis() - base; } } }
ok,看了一下源碼,還是比較容易理解的,其實就是調(diào)用了現(xiàn)場wait方法實現(xiàn)線程同步的
“java線程join方法的使用方法”的內(nèi)容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業(yè)相關(guān)的知識可以關(guān)注創(chuàng)新互聯(lián)網(wǎng)站,小編將為大家輸出更多高質(zhì)量的實用文章!
網(wǎng)站名稱:java線程join方法的使用方法
標(biāo)題來源:http://aaarwkj.com/article46/pdpieg.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供品牌網(wǎng)站制作、營銷型網(wǎng)站建設(shè)、建站公司、App設(shè)計、網(wǎng)站內(nèi)鏈、小程序開發(fā)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)