判断线程是否停止的状态
Thread.java类里面提供了两种方法。
1)this.interrupted():测试当前线程是否已经中断
2)this.isInterrupted():测试线程是否已经中断
测试如下
package com.cky.thread; /** * Created by edison on 2017/11/28. */ public class MyThread11 extends Thread{ @Override public void run() { super.run(); for (int i = 0; i < 50000; i++) { System.out.println("i="+i); } } }
1 package com.cky.test; 2 3 import com.cky.thread.MyThread11; 4 5 /** 6 * Created by edison on 2017/11/28. 7 */ 8 public class Test18 { 9 public static void main(String[] args) { 10 try { 11 MyThread11 th = new MyThread11(); 12 th.start(); 13 Thread.sleep(2000); 14 th.interrupt(); 15 System.out.println("是否停止1="+ th.isInterrupted()); 16 System.out.println("是否停止2="+ th.isInterrupted()); 17 } catch (InterruptedException e) { 18 e.printStackTrace(); 19 } 20 } 21 }
i=49990 i=49991 i=49992 i=49993 i=49994 i=49995 i=49996 i=49997 i=49998 i=49999 是否停止1=false 是否停止2=false
结果分析:
结果表面当前线程并未停止,因为这个当前线程是main线程,所以打印了两个false.
测试
1 package com.cky.test; 2 3 /** 4 * Created by edison on 2017/11/28. 5 */ 6 public class Test19 { 7 public static void main(String[] args) { 8 Thread.currentThread().interrupt(); 9 System.out.println("是否停止1="+Thread.interrupted()); 10 System.out.println("是否停止2="+Thread.interrupted()); 11 System.out.println("end"); 12 13 } 14 }
1 C:\itsoft\jdk\bin\java -Didea.launcher.port=7534 "-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.Test19 2 是否停止1=true 3 是否停止2=false 4 end 5 6 Process finished with exit code 0
结果分析:上面结果第一次判断为true,说明当前main线程已经中断,但是第二次调用变成了false.
官方文档解释:线程的中断状态由该方法清除,所以两次调用的话,第二次会返回false.
再来测试isInterrupted()
1 package com.cky.test; 2 3 /** 4 * Created by edison on 2017/11/28. 5 */ 6 public class Test19 { 7 public static void main(String[] args) { 8 Thread.currentThread().interrupt(); 9 System.out.println("是否停止1="+Thread.currentThread().isInterrupted()); 10 System.out.println("是否停止2="+Thread.currentThread().isInterrupted()); 11 System.out.println("end"); 12 13 } 14 }
C:\itsoft\jdk\bin\java -Didea.launcher.port=7541 "-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.Test19 是否停止1=true 是否停止2=true end Process finished with exit code 0
结果分析:
方法isInterrupted()并未清除状态标识,所以打印了两个false.
时间: 2024-10-09 19:32:19