1.7.4在沉睡中停止
在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:\itsoft\jdk\bin\java -Didea.launcher.port=7537 "-Didea.launcher.bin.path=C:\itsoft\idea\IntelliJ IDEA 2016.3.3\bin" -Dfile.encoding=UTF-8 -classpath "C:\itsoft\jdk\jre\lib\charsets.jar;C:\itsoft\jdk\jre\lib\deploy.jar;C:\itsoft\jdk\jre\lib\ext\access-bridge-32.jar;C:\itsoft\jdk\jre\lib\ext\cldrdata.jar;C:\itsoft\jdk\jre\lib\ext\dnsns.jar;C:\itsoft\jdk\jre\lib\ext\jaccess.jar;C:\itsoft\jdk\jre\lib\ext\jfxrt.jar;C:\itsoft\jdk\jre\lib\ext\localedata.jar;C:\itsoft\jdk\jre\lib\ext\nashorn.jar;C:\itsoft\jdk\jre\lib\ext\sunec.jar;C:\itsoft\jdk\jre\lib\ext\sunjce_provider.jar;C:\itsoft\jdk\jre\lib\ext\sunmscapi.jar;C:\itsoft\jdk\jre\lib\ext\sunpkcs11.jar;C:\itsoft\jdk\jre\lib\ext\zipfs.jar;C:\itsoft\jdk\jre\lib\javaws.jar;C:\itsoft\jdk\jre\lib\jce.jar;C:\itsoft\jdk\jre\lib\jfr.jar;C:\itsoft\jdk\jre\lib\jfxswt.jar;C:\itsoft\jdk\jre\lib\jsse.jar;C:\itsoft\jdk\jre\lib\management-agent.jar;C:\itsoft\jdk\jre\lib\plugin.jar;C:\itsoft\jdk\jre\lib\resources.jar;C:\itsoft\jdk\jre\lib\rt.jar;C:\多线程核心技术\第一章\out\production\第一章;C:\itsoft\idea\IntelliJ IDEA 2016.3.3\lib\idea_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了