public class ThreadStop_now {
private static class MyThread extends Thread{
public void run() {
while (!isInterrupted()) {
System.out.println(Thread.currentThread().getName()+" is run ");
}
}
}
public static void main(String[] args) throws InterruptedException {
Thread t = new MyThread();
t.start();
Thread.sleep(1000);
t.interrupt();
}
}
结果是线程会终止
但是如果把new MyThread()换成new Thread(new MyThread()),最后的结果就是线程无法终止
设置成后台线程试试:t.setDaemon(true);