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

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

interrupt并不能中斷線程那它為啥叫interrupt啊

interrupt清楚阻塞狀態(tài)并拋出異常,這和中斷有啥關(guān)系?

正在回答

4 回答

3-3 Java線程停止廣為流傳的錯(cuò)誤方法---interrupt方法

這節(jié)課老師講的初衷是interrupt方法不能正確地停止線程,是停止.interrupt方法本身就是中斷線程的意思.能中斷但不能停止線程.

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

你這個(gè)問(wèn)題好問(wèn)的笑死我了,那你咋不問(wèn)stop的意思是停止,他為什么不能停止呢

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

是中斷不是終斷;

比如講師在本節(jié)課中的一個(gè)例子:

package?com.imooc.demo;

public?class?WrongWayStopThread?extends?Thread?{

	public?static?void?main(String[]?args)?{
		WrongWayStopThread?thread??=?new?WrongWayStopThread();
		System.out.println("Starting?thread");
		thread.start();
		try?{
			Thread.sleep(3000);
		}?catch?(InterruptedException?e)?{
			//?TODO?Auto-generated?catch?block
			e.printStackTrace();
		}
		System.out.println("Tnterrupting?thread");
		thread.interrupt();
		try?{
			Thread.sleep(3000);
		}?catch?(InterruptedException?e)?{
			//?TODO?Auto-generated?catch?block
			e.printStackTrace();
		}
		System.out.println("Stopping?thread");
	}
	@Override
	public?void?run()?{
//		在線程之中,我們定義了一個(gè)無(wú)限循環(huán)的while()結(jié)構(gòu),在while()循環(huán)之中,我輸出線程正在運(yùn)行的信息
//		然后嵌套一個(gè)空的while()循環(huán),減少屏幕輸出,使得每秒只輸出一行信息,它的作用大致相當(dāng)于Thread.sleep(1000)
//		但是為什么不是Thread.sleep(1000)?
//		
		while?(!this.isInterrupted())?{
			System.out.println("Thread?is?running");
			long?time?=?System.currentTimeMillis();
			while?((System.currentTimeMillis()?-?time)?<?1000)?{
//			try?{
//				Thread.sleep(1000);
//			}?catch?(InterruptedException?e)?{
//				//?TODO?Auto-generated?catch?block
//				e.printStackTrace();
//			}	
			}
		}
	}
}

我們知道m(xù)ain也一個(gè)主線程,在執(zhí)行到程序的入口,也就是main()方法的時(shí)候,程序就會(huì)產(chǎn)生一個(gè)線程,然后調(diào)用start()的時(shí)候會(huì)產(chǎn)生一個(gè)線程,這兩個(gè)線程并行,調(diào)用Thread.sleep(3000),指的是讓main這個(gè)主線程休眠3秒,我們的thread線程才搶占資源運(yùn)行,隨著main線程3秒后的恢復(fù),thread線程就恢復(fù)了搶占資源的狀態(tài),然后main線程讀取到了thread.interrupt(),也就是說(shuō)thread線程進(jìn)行了終止,那么isInterrupted()的返回值就是true,所以while()循環(huán)會(huì)停止,也就是說(shuō),thread線程結(jié)束了,然后經(jīng)過(guò)Thread.sleep(),主線程就又停止了3秒,然后輸出線程停止。

一家之言,敬請(qǐng)指正!

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

官方就是這么叫的。。。咱們也沒(méi)辦法啊

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

舉報(bào)

0/150
提交
取消

interrupt并不能中斷線程那它為啥叫interrupt啊

我要回答 關(guān)注問(wèn)題
微信客服

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

幫助反饋 APP下載

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

公眾號(hào)

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