import java.io.IOException; public class Test { public static void main(String[] args) throws InterruptedException, IOException { ThreadInterrupt t = new ThreadInterrupt(); Thread thread = new Thread(t); thread.start(); Thread.sleep(5000); //5秒后thread被打断 thread.interrupt(); System.out.println("线程退出运行!"); } } public class ThreadInterrupt implements Runnable{ public void run() { for(int i=0;i<500;i++){ System.out.println("i am a running thread"+(i)); try { Thread.sleep(2000); } catch (InterruptedException e) { System.out.println("睡眠被打断!"); } } } } interrupt()方法困惑?
把某个线程interrupt()后,竟然还继续执行!那这个方法究竟有啥用呢?
最新推荐文章于 2023-04-25 12:02:34 发布