线程中断测试
package com.fh.interview;/*** @author* @create 2018-06-12 下午10:15**/
public class Interrent {public static void main(String[] args) {Thread run = new Thread(new Runnable() {@Overridepublic void run() {int i=0;while (true){
// System.out.println("running");i++;}}});Thread sleep = new Thread(new Runnable() {@Overridepublic void run() {try {Thread.sleep(100000);}catch (Exception e){e.printStackTrace();}}});run.start();sleep.start();run.interrupt();sleep.interrupt();System.out.println("*************");System.out.println("run state:"+run.isInterrupted());System.out.println("**************");System.out.println("sleep state:"+sleep.isInterrupted());System.out.println("***************");System.out.println("run state:"+run.isAlive());System.out.println("****************");System.out.println("sleep state:"+sleep.isAlive());/* *************run state:true**************sleep state:true***************run state:true****************sleep state:true正在運行的任務(wù)調(diào)用interrupt,并不會停止線程的運行處于sleep,join,wait的線程,會拋出中斷異常*/}
}
?
轉(zhuǎn)載于:https://www.cnblogs.com/nihaofenghao/p/9175465.html
總結(jié)
- 上一篇: 回溯经典(指定位置N皇后问题)
- 下一篇: 第二十天