1.7.4在沉睡中中止

在sleep狀態下,中止線程。會進入catch語句,而且清除中止狀態值,使其變成falsejava

 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()api

下面實驗先中止線程,在進入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

結果分析:app

這邊因爲主線程先執行完了代碼,給子線程打了中止標記,當子線程執行時線程中止了,再執行sleep方法,就會進入catch了ide

相關文章
相關標籤/搜索