public class WrongExit extends Thread{
public void run(){
while(!this.isInterrupted()){
System.out.println("Thread is continuing");
long time = System.currentTimeMillis();
while((System.currentTimeMillis()-time<1000)){
}
}
}
public static void main(String[] args){
WrongExit WE = new WrongExit();
System.out.println("----------线程开始---------");
WE.start();
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
System.out.println("********3秒后终止线程********");
Thread.interrupted();
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
System.out.println("线程结束");
}
}用WE.interrupt()。 Thread.interrupted()错了。
变量首写字母 建议小写 驼峰式命名 用你的变量去interrupt,
Thread.interrupted()
进程的interrupt状态被清除(cleard)而非被设置(set)。
因为你定义的名字是WE