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...");
}
}
2016-05-20
哈哈,微笑不語
2016-03-16
? 哎呦,這個還是用了標志位設(shè)置的。。。
2015-10-14
這句是什么意思???是字面意思吧?就是thread在sleep的時候不去取stop的值?