public class MyThread extends Thread { public void run(){ while (true){ if(this.isInterrupted()){ System.out.println("线程被停止了!"); return; } System.out.println("Time: " + System.currentTimeMillis()); } } }
public class Run { public static void main(String args[]) throws InterruptedException { Thread thread = new MyThread(); thread.start(); Thread.sleep(2000); thread.interrupt(); } }