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...");
}
}