/** * 如何中断实现Runnable类型的线程 */ public class EndRunnable { private static class UseRunnable implements Runnable{ @Override public void run() { Thread thread = Thread.currentThread(); String threadName = thread.getName(); while(!thread.isInterrupted()) { try { thread.sleep(100); System.out.println(threadName + " is running !"); } catch (InterruptedException e) { System.out.println(threadName+" throws exception interruptFlag is " + thread.isInterrupted()); thread.interrupt(); e.printStackTrace(); } } System.out.println(threadName+" interruptFlag is " + thread.isInterrupted()); } } public static void main(String[] args) throws InterruptedException { UseRunnable useRunnable = new UseRunnable(); Thread endThread = new Thread(useRunnable,"endThread"); endThread.start(); Thread.sleep(500); endThread.interrupt(); } }
如何中断实现Runnable类型的线程
最新推荐文章于 2023-02-07 18:15:34 发布