public class WrongWayStopThread extends Thread{ public void run(){ System.out.println("线程正在运行"); long time = System.currentTimeMillis(); while((System.currentTimeMillis()-time)<1000){} } public static void main(String[] args) { WrongWayStopThread wwst = new WrongWayStopThread(); System.out.println("启动线程 "); wwst.start(); try { sleep(3000); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println("中断线程"); wwst.interrupt(); try { sleep(3000); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println("线程结束了"); } }
自己找到错误了,run方法中没写while循环