用interrupt中斷程序html
public class InterruptionInJava implements Runnable{ @Override public void run() { while (true) { if (Thread.currentThread().isInterrupted()) { System.out.println("Yes!! I'm Interupted, but I'm still running"); } else { } } // System.out.println("Oh, myGod!"); } public static void main(String[] args) { Thread thread = new Thread(new InterruptionInJava(), "testThread"); thread.start(); try { Thread.sleep(1000); } catch (InterruptedException e) { } System.out.println("Begin Interupt..."); thread.interrupt(); System.out.println("End Interupt..."); } }
輸出java
Yes!! I'm Interupted, but I'm still running Yes!! I'm Interupted, but I'm still running Yes!! I'm Interupted, but I'm still running Yes!! I'm Interupted, but I'm still running Yes!! I'm Interupted, but I'm still running Yes!! I'm Interupted, but I'm still running Yes!! I'm Interupted, but I'm still running .....
問題:雖然是被中斷狀態,但實際並未中斷bash
在java中主要有3個相關方法,interrupt(),isInterrupted()和interrupted()。ide
處於被中斷狀態時,return 或 bread 中斷線程spa
public class InterruptionInJava implements Runnable{ @Override public void run() { while (true) { if (Thread.currentThread().isInterrupted()) { System.out.println("Yes!! I'm Interupted, but I'm still running"); return; } else { } } } public static void main(String[] args) { Thread thread = new Thread(new InterruptionInJava(), "testThread"); thread.start(); try { Thread.sleep(1000); } catch (InterruptedException e) { } System.out.println("Begin Interupt..."); thread.interrupt(); System.out.println("End Interupt..."); } }
public class InterruptionInJava2 implements Runnable{ private volatile static boolean on = false; @Override public void run() { while (!on) { try { System.out.println("begin Sleep"); Thread.sleep(10000000); System.out.println("end Sleep"); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println("Oh, myGod!"); } } public static void main(String[] args) { Thread thread = new Thread(new InterruptionInJava2(), "testThread"); thread.start(); try { System.out.println("main Begin sleep"); Thread.sleep(5000); System.out.println("main End sleep"); } catch (InterruptedException e) { } InterruptionInJava2.on = true; } }
使用靜態變量on線程
可是在run方法中Thread.sleep(10000000),😱什麼時候結束?code
public class InterruptionInJava2 implements Runnable{ private volatile static boolean on = false; @Override public void run() { while (!on) { try { System.out.println("begin Sleep"); Thread.sleep(10000000); System.out.println("end Sleep"); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println("Oh, myGod!"); } } public static void main(String[] args) { Thread thread = new Thread(new InterruptionInJava2(), "testThread"); thread.start(); try { System.out.println("main Begin sleep"); Thread.sleep(5000); System.out.println("main End sleep"); } catch (InterruptedException e) { } InterruptionInJava2.on = true; System.out.println("Begin Interupt..."); thread.interrupt(); System.out.println("End Interupt..."); } }
輸出htm
begin Sleep
main Begin sleep
main End sleep
Begin Interupt...
End Interupt...
java.lang.InterruptedException: sleep interrupted
at java.lang.Thread.sleep(Native Method)
at com.jihite.templet.JavaBase.InterruptionInJava2.run(InterruptionInJava2.java:11)
at java.lang.Thread.run(Thread.java:748)
Oh, myGod!
Process finished with exit code 0