在sleep状态下,停止线程。会进入catch语句,并且清除停止状态值,使其变成false
1 package com.cky.thread; 2 3 /** 4 * Created by edison on 2017/12/3. 5 */ 6 public class MyThread extends Thread{ 7 @Override 8 public void run() { 9 super.run(); 10 try { 11 System.out.println("run begin"); 12 Thread.sleep(20000); 13 System.out.println("run end"); 14 } catch (InterruptedException e) { 15 System.out.println("沉睡中被停只进入catch "+this.isInterrupted()); 16 e.printStackTrace(); 17 } 18 } 19 }
1 package com.cky.test; 2 3 import com.cky.thread.MyThread; 4 5 /** 6 * Created by edison on 2017/12/3. 7 */ 8 public class Test { 9 public static void main(String[] args) { 10 try { 11 MyThread th = new MyThread(); 12 th.start(); 13 Thread.sleep(200); 14 th.interrupt(); 15 } catch (InterruptedException e) { 16 System.out.println("main catch"); 17 e.printStackTrace(); 18 } 19 20 21 } 22 }
C:itsoftjdkinjava -Didea.launcher.port=7537 "-Didea.launcher.bin.path=C:itsoftideaIntelliJ IDEA 2016.3.3in" -Dfile.encoding=UTF-8 -classpath "C:itsoftjdkjrelibcharsets.jar;C:itsoftjdkjrelibdeploy.jar;C:itsoftjdkjrelibextaccess-bridge-32.jar;C:itsoftjdkjrelibextcldrdata.jar;C:itsoftjdkjrelibextdnsns.jar;C:itsoftjdkjrelibextjaccess.jar;C:itsoftjdkjrelibextjfxrt.jar;C:itsoftjdkjrelibextlocaledata.jar;C:itsoftjdkjrelibext ashorn.jar;C:itsoftjdkjrelibextsunec.jar;C:itsoftjdkjrelibextsunjce_provider.jar;C:itsoftjdkjrelibextsunmscapi.jar;C:itsoftjdkjrelibextsunpkcs11.jar;C:itsoftjdkjrelibextzipfs.jar;C:itsoftjdkjrelibjavaws.jar;C:itsoftjdkjrelibjce.jar;C:itsoftjdkjrelibjfr.jar;C:itsoftjdkjrelibjfxswt.jar;C:itsoftjdkjrelibjsse.jar;C:itsoftjdkjrelibmanagement-agent.jar;C:itsoftjdkjrelibplugin.jar;C:itsoftjdkjrelib esources.jar;C:itsoftjdkjrelib t.jar;C:多线程核心技术第一章outproduction第一章;C:itsoftideaIntelliJ IDEA 2016.3.3libidea_rt.jar" com.intellij.rt.execution.application.AppMain com.cky.test.Test run begin java.lang.InterruptedException: sleep interrupted 沉睡中被停只进入catch false at java.lang.Thread.sleep(Native Method) at com.cky.thread.MyThread.run(MyThread.java:12) Process finished with exit code 0
前一个实验是先sleep后,在执行interrupt()
下面实验先停止线程,在进入sleep
1 package com.cky.thread; 2 3 /** 4 * Created by edison on 2017/12/3. 5 */ 6 public class MyThread extends Thread{ 7 @Override 8 public void run() { 9 super.run(); 10 try { 11 for (int i = 0; i < 100000; i++) { 12 System.out.println("i="+(i+1)); 13 } 14 System.out.println("run begin"); 15 Thread.sleep(20000); 16 System.out.println("run end"); 17 } catch (InterruptedException e) { 18 System.out.println("先停止再遇到了sleep进入catch "); 19 e.printStackTrace(); 20 } 21 } 22 }
1 package com.cky.test; 2 3 import com.cky.thread.MyThread; 4 5 /** 6 * Created by edison on 2017/12/3. 7 */ 8 public class Test { 9 public static void main(String[] args) { 10 MyThread th = new MyThread(); 11 th.start(); 12 th.interrupt(); 13 14 15 16 } 17 }
i=99997 i=99998 i=99999 i=100000 run begin 先停止再遇到了sleep进入catch java.lang.InterruptedException: sleep interrupted at java.lang.Thread.sleep(Native Method) at com.cky.thread.MyThread.run(MyThread.java:15) Process finished with exit code 0
结果分析:
这边由于主线程先执行完了代码,给子线程打了停止标记,当子线程执行时线程停止了,再执行sleep方法,就会进入catch了