亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定

個人練習:正確使用interrupt()停止進程

package?com.imooc.concurrent.base;

public?class?InterruptWayStopThread?extends?Thread{
	
	volatile?boolean?stop?=?false;
	public?static?void?main(String[]?args)?{
		InterruptWayStopThread?thread?=?new?InterruptWayStopThread();
		System.out.println("Starting?thread...");
		thread.start();
		try?{
			Thread.sleep(3000);
		}?catch?(InterruptedException?e)?{
			//?TODO?Auto-generated?catch?block
			e.printStackTrace();
		}
		System.out.println("Interrupting?thread...");
		thread.stop?=?true;????????//線程要是處于阻塞模式,將不會檢查此處的變量
		thread.interrupt();
		try?{
			Thread.sleep(3000);
		}?catch?(InterruptedException?e)?{
			e.printStackTrace();
		}
		System.out.println("Stopping?appplication...");		
	}
	
	@Override
	public?void?run()?{
		while(!stop){
			System.out.println("Thread?is?running...");
			try?{
				Thread.sleep(1000);
			}?catch?(InterruptedException?e)?{
				System.out.println("Thread?interrupted..");
			}
		}
		System.out.println("Thread?exiting?under?requestion...");
				
	}

}


正在回答

3 回答

哈哈,微笑不語

0 回復 有任何疑惑可以回復我~

? 哎呦,這個還是用了標志位設置的。。。

0 回復 有任何疑惑可以回復我~
thread.stop?=?true;????????//線程要是處于阻塞模式,將不會檢查此處的變量

這句是什么意思???是字面意思吧?就是thread在sleep的時候不去取stop的值?

0 回復 有任何疑惑可以回復我~

舉報

0/150
提交
取消

個人練習:正確使用interrupt()停止進程

我要回答 關注問題
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號