/** * 如何安全中断继承Thread的线程 */ public class EndThread { private static class UseThread extends Thread{ public UseThread(String name) { super(name); } @Override public void run() { String threadName = Thread.currentThread().getName(); while(!isInterrupted()) { try { Thread.sleep(100); System.out.println(threadName+" is running !"); } catch (InterruptedException e) { System.out.println(threadName+"throws exception interrupteFlag is "+isInterrupted()); interrupt(); e.printStackTrace(); } } System.out.println(threadName+" interrupteFlag is "+isInterrupted()); } } public static void main(String[] args) throws InterruptedException { Thread endThread = new UseThread("endThread"); endThread.start(); Thread.sleep(500); endThread.interrupt(); } }
如何安全中断继承Thread的线程
最新推荐文章于 2021-02-24 11:45:21 发布