第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定

sleep的問(wèn)題

http://img1.sycdn.imooc.com//59c0d5cc0001497504000179.jpg

軍隊(duì)作戰(zhàn)到什么時(shí)候才會(huì)進(jìn)行下一步Thread.sleep(50)呢

正在回答

5 回答

同問(wèn)?

0 回復(fù) 有任何疑惑可以回復(fù)我~

了解了線程,可以去了解下 隊(duì)列, 對(duì)于處理高并發(fā),和提高業(yè)務(wù)數(shù)據(jù)吞吐量會(huì)很有用。

0 回復(fù) 有任何疑惑可以回復(fù)我~
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í)行,看看還有什么可以做");
	}
	
}


0 回復(fù) 有任何疑惑可以回復(fù)我~

誰(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方法的。

0 回復(fù) 有任何疑惑可以回復(fù)我~

我覺(jué)得應(yīng)該是上面兩個(gè)線程都結(jié)束了,才繼續(xù)進(jìn)行下面的sleep,你可以將50設(shè)置的大一點(diǎn),比如好幾毫秒,然后你可以看一下運(yùn)行結(jié)果。

0 回復(fù) 有任何疑惑可以回復(fù)我~
#1

16k閃存大腦3954634 提問(wèn)者

上面兩個(gè)線程是while(true)的死循環(huán)呀,怎么執(zhí)行的完
2017-09-19 回復(fù) 有任何疑惑可以回復(fù)我~
#2

慕瓜3258604

111
2018-03-02 回復(fù) 有任何疑惑可以回復(fù)我~
#3

慕瓜3258604 回復(fù) 慕瓜3258604

111111
2018-03-02 回復(fù) 有任何疑惑可以回復(fù)我~
#4

慕瓜3258604 回復(fù) 慕瓜3258604

222
2018-03-02 回復(fù) 有任何疑惑可以回復(fù)我~
查看1條回復(fù)

舉報(bào)

0/150
提交
取消
深入淺出Java多線程
  • 參與學(xué)習(xí)       186071    人
  • 解答問(wèn)題       498    個(gè)

帶你一起深入淺出多線程,掌握基礎(chǔ),展望進(jìn)階路線

進(jìn)入課程
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)