線程join
老師您好,我去掉了舞臺中軍隊停止等待程咬金join的方法,運(yùn)行以后發(fā)現(xiàn)只有主線程停止執(zhí)行,其它的子線程會繼續(xù)執(zhí)行,是不是join方法只會阻塞主線程的執(zhí)行而不會影響其它子線程的執(zhí)行呢?
老師您好,我去掉了舞臺中軍隊停止等待程咬金join的方法,運(yùn)行以后發(fā)現(xiàn)只有主線程停止執(zhí)行,其它的子線程會繼續(xù)執(zhí)行,是不是join方法只會阻塞主線程的執(zhí)行而不會影響其它子線程的執(zhí)行呢?
2015-04-05
舉報
2015-08-02
你的理解很對:當(dāng)某個線程使用join()方法加入到另一個線程時,另一個線程會等待該線程執(zhí)行完畢后再繼續(xù)執(zhí)行。
程咬金線程通過join()方法加入到舞臺線程,所以只有舞臺線程會等待。隋軍和農(nóng)民軍線程的結(jié)束,是在舞臺線程中加入armyTaskOfSuiDynasty.keepRunning = false;??armyTaskOfRevolt.keepRunning = false;然后舞臺線程又sleep(2000),讓隋軍和農(nóng)民軍線程結(jié)束最后一輪(while()循環(huán)),讓程咬金線程獨(dú)自執(zhí)行。
2015-04-08
這樣分清當(dāng)前線程。當(dāng)前軍隊線程正在執(zhí)行,在舞臺里面通過軍隊線程上調(diào)用join就是講舞臺阻塞,等待軍隊完成。
一下是Oracle的文檔說明,你可以體會一下。
Joins
The?join?method allows one thread to wait for the completion of another. If?t?is a?Thread?object whose thread is currently executing,
t.join();
causes the current thread to pause execution until?t's thread terminates.?