1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
| { Thread t1 = new Thread(() -> { while (true) { if(Thread.currentThread().isInterrupted()) { System.out.println(Thread.currentThread().getName()+"\t " + "中断标志位:"+Thread.currentThread().isInterrupted()+" 程序停止"); break; }
try { Thread.sleep(200); } catch (InterruptedException e) { Thread.currentThread().interrupt(); e.printStackTrace(); }
System.out.println("-----hello InterruptDemo3"); } }, "t1"); t1.start();
try { TimeUnit.SECONDS.sleep(1); } catch (InterruptedException e) { e.printStackTrace(); }
new Thread(() -> t1.interrupt(),"t2").start(); }
|