package book.test; import org.springframework.cglib.proxy.Proxy; import java.io.ByteArrayOutputStream; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; public class IsInterruptedDemo1 { public static class DaemonT extends Thread{ @Override public void run() { for (int i = 0; i < 10; i++) { System.out.println("i="+(i+1)); if(this.isInterrupted()){ System.out.println("通過this.isInterrupted()檢測到中斷"); System.out.println("線程名稱="+Thread.currentThread().getName()+"第0個isInterrupted() "+Thread.currentThread().isInterrupted()); // System.out.println("第一個interrupted()"+this.interrupted()); // System.out.println("第二個interrupted()"+this.interrupted()); System.out.println("線程名稱="+Thread.currentThread().getName()+"第1個isInterrupted() "+this.isInterrupted()); break; } } System.out.println("因爲檢測到中斷,所以跳出循環,線程到這裏結束,因爲後面沒有內容了"); } } public static void main(String[] args) throws InterruptedException { DaemonT daemonT = new DaemonT(); // daemonT.setDaemon(true); daemonT.start(); Thread.currentThread().interrupt();//當前線程設置中斷標識 daemonT.interrupt();//直接爲調用的線程對象設置中斷標識 System.out.println("線程名稱="+Thread.currentThread().getName()+"是否被中斷"+Thread.currentThread().isInterrupted()); System.out.println("第一次調用:"+daemonT.interrupted());//雖然是daemonT調用的,但是檢測的是當前線程,(interrupted() 方法就是檢測當前線程 return currentThread().isInterrupted(true);) System.out.println("在調用daemonT.interrupted()之後的狀態"+"線程名稱="+Thread.currentThread().getName()+"是否被中斷"+Thread.currentThread().isInterrupted()); System.out.println(" daemonT.interrupt() 第二次調用:"+daemonT.isInterrupted()); System.out.println(" daemonT.isAlive() :"+daemonT.isAlive()); Thread.sleep(2000); /*System.out.println("=============================================="); boolean interrupted1 = Thread.currentThread().isInterrupted();//獲取當前線程(無論是誰調用都是獲取獲取的當前線程)的中斷狀態 System.out.println(interrupted1); boolean interrupted = Thread.interrupted();//獲取當前線程(無論是誰調用都是獲取獲取的當前線程)的中斷狀態,相比於isInterrupted(),interrupted()會清除中斷標識 System.out.println(interrupted); Thread.currentThread().interrupt();//爲調用此方法的線程設置中斷標識 boolean interrupted3 = Thread.currentThread().isInterrupted();//獲取當前線程(無論是誰調用都是獲取獲取的當前線程)的中斷狀態 System.out.println(interrupted3); boolean interrupted4 = Thread.interrupted();//獲取當前線程(無論是誰調用都是獲取獲取的當前線程)的中斷狀態,相比於isInterrupted(),interrupted()會清除中斷標識 System.out.println(interrupted4); boolean interrupted5 = Thread.currentThread().isInterrupted();//獲取當前線程(無論是誰調用都是獲取獲取的當前線程)的中斷狀態 System.out.println(interrupted5);*/ try { byte[] bytes = input2byte(new FileInputStream("F:\\ideawork\\zookeeper_paxos\\zookeeper_book\\src\\main\\java\\book\\test\\velocity\\VelocityGenerate.java")); System.out.println(" bytes :"+bytes.length); } catch (IOException e) { e.printStackTrace(); } } /** * 總結: * interrupt(),直接爲調用的線程對象設置中斷標識 * interrupted(),不管是哪個對象調用,都是返回當前線程的中斷標識,如果發現當前線程被中斷,會清除中斷標識 * isInterrupted(),返回對象調用者的中斷標識 */ private static final byte[] input2byte(InputStream inStream) throws IOException { ByteArrayOutputStream swapStream = new ByteArrayOutputStream(); byte[] buff = new byte[100]; int rc = 0; while ((rc = inStream.read(buff, 0, 100)) > 0) { swapStream.write(buff, 0, rc); } byte[] in2b = swapStream.toByteArray(); return in2b; } }
運行結果:
線程名稱=main是否被中斷true
i=1
第一次調用:true
通過this.isInterrupted()檢測到中斷
在調用daemonT.interrupted()之後的狀態線程名稱=main是否被中斷false
線程名稱=Thread-0第0個isInterrupted() true
daemonT.interrupt() 第二次調用:true
線程名稱=Thread-0第1個isInterrupted() true
因爲檢測到中斷,所以跳出循環,線程到這裏結束,因爲後面沒有內容了
daemonT.isAlive() :true
bytes :197
總結:
interrupt(),直接爲調用的線程對象設置中斷標識
interrupted(),不管是哪個對象調用,都是返回當前線程的中斷標識,如果發現當前線程被中斷,會清除中斷標識
isInterrupted(),返回對象調用者的中斷標識