課程
/后端開(kāi)發(fā)
/Java
/深入淺出Java多線程
軍隊(duì)作戰(zhàn)到什么時(shí)候才會(huì)進(jìn)行下一步Thread.sleep(50)呢
2017-09-19
源自:深入淺出Java多線程 2-5
正在回答
同問(wèn)?
了解了線程,可以去了解下 隊(duì)列, 對(duì)于處理高并發(fā),和提高業(yè)務(wù)數(shù)據(jù)吞吐量會(huì)很有用。
package?com.junbao.thread; import?java.util.ArrayList; import?java.util.List; import?java.util.NoSuchElementException; import?java.util.concurrent.ArrayBlockingQueue; import?java.util.concurrent.BlockingQueue; import?java.util.concurrent.ExecutorService; import?java.util.concurrent.Executors; public?class?ThreadTest?{ static??ExecutorService??executorService??=??Executors?.?newFixedThreadPool?(5)?; @SuppressWarnings("deprecation") public?static?void?main(String[]?args)?throws?InterruptedException?{ ThreadTest?test?=?new?ThreadTest(); List<Integer>?numList?=?new?ArrayList<Integer>(); for?(int?i?=?0;?i?<?10;?i++)?{ numList.add(i); } //?隊(duì)列 ?final?BlockingQueue<Integer>?queue?=?new?ArrayBlockingQueue<Integer>(numList.size()); //?放入 try?{ for?(Integer?ints?:?numList)?{ queue.put(ints); } }?catch?(Exception?e)?{ //?TODO:?handle?exception } final?List<Integer>?outList?=?new?ArrayList<Integer>(); for?(int?j?=?0;?j?<?5;?j++)?{ new?Thread()?{ public?void?run()?{ while?(true)?{ try?{ //?將此處的睡眠時(shí)間分別改為100和1000,觀察運(yùn)行結(jié)果 Thread.sleep((int)?(Math.random()?*?(1000?-?100)?+?100)); String?name1?=?Thread.currentThread().getName(); System.out.println(name1?+?"準(zhǔn)備取數(shù)據(jù)!"); Integer?num?=?queue.take();//?如果沒(méi)有了數(shù)據(jù),就會(huì)阻塞 String?name2?=?Thread.currentThread().getName(); outList.add(num); System.out.println(name2?+?"已經(jīng)取走數(shù)據(jù),"?+?"隊(duì)列目前有"?+?queue.size()?+?"個(gè)數(shù)據(jù)"); }?catch?(InterruptedException?e)?{ e.printStackTrace(); }?catch?(NoSuchElementException?e1)?{ //?TODO:?handle?exception } } } }.start(); } try?{ while?(true)?{ if?(queue.isEmpty())?{ System.out.println("空了,可以執(zhí)行其他的任務(wù)了:打印輸出轉(zhuǎn)以后的列表"); test.showList(outList); System.out.println(Thread.currentThread().getState()); break; } } }?catch?(Exception?e)?{ //?TODO:?handle?exception } test(); } public?void?showList(List<Integer>?list)?{ System.out.println(list.toString()); } public?static?void?test(){ System.out.println("繼續(xù)往下執(zhí)行,看看還有什么可以做"); } }
誰(shuí)說(shuō)一定要結(jié)束才會(huì)執(zhí)行sleep,不是說(shuō)你能很準(zhǔn)確知道作戰(zhàn)到什么時(shí)候會(huì)進(jìn)行下一步,兩個(gè)線程運(yùn)行是狀態(tài)是交互的,然后你還有個(gè)stage線程,三個(gè)線程都是獨(dú)立的。你這兩個(gè)運(yùn)行會(huì),他還是會(huì)運(yùn)行stage線程里的sleep方法的。
我覺(jué)得應(yīng)該是上面兩個(gè)線程都結(jié)束了,才繼續(xù)進(jìn)行下面的sleep,你可以將50設(shè)置的大一點(diǎn),比如好幾毫秒,然后你可以看一下運(yùn)行結(jié)果。
16k閃存大腦3954634 提問(wèn)者
慕瓜3258604
慕瓜3258604 回復(fù) 慕瓜3258604
舉報(bào)
帶你一起深入淺出多線程,掌握基礎(chǔ),展望進(jìn)階路線
2 回答sleep問(wèn)題
2 回答sleep()使用問(wèn)題
4 回答sleep()
3 回答關(guān)于sleep();
3 回答關(guān)于Sleep對(duì)interrupt的影響
Copyright ? 2025 imooc.com All Rights Reserved | 京ICP備12003892號(hào)-11 京公網(wǎng)安備11010802030151號(hào)
購(gòu)課補(bǔ)貼聯(lián)系客服咨詢優(yōu)惠詳情
慕課網(wǎng)APP您的移動(dòng)學(xué)習(xí)伙伴
掃描二維碼關(guān)注慕課網(wǎng)微信公眾號(hào)
2018-04-17
同問(wèn)?
2017-10-29
了解了線程,可以去了解下 隊(duì)列, 對(duì)于處理高并發(fā),和提高業(yè)務(wù)數(shù)據(jù)吞吐量會(huì)很有用。
2017-10-29
2017-09-22
誰(shuí)說(shuō)一定要結(jié)束才會(huì)執(zhí)行sleep,不是說(shuō)你能很準(zhǔn)確知道作戰(zhàn)到什么時(shí)候會(huì)進(jìn)行下一步,兩個(gè)線程運(yùn)行是狀態(tài)是交互的,然后你還有個(gè)stage線程,三個(gè)線程都是獨(dú)立的。你這兩個(gè)運(yùn)行會(huì),他還是會(huì)運(yùn)行stage線程里的sleep方法的。
2017-09-19
我覺(jué)得應(yīng)該是上面兩個(gè)線程都結(jié)束了,才繼續(xù)進(jìn)行下面的sleep,你可以將50設(shè)置的大一點(diǎn),比如好幾毫秒,然后你可以看一下運(yùn)行結(jié)果。