菜鳥面試官:如何停止一個正在運行的線程?我蒙了~
閱讀本文大概需要 8 分鐘。
來自:cnblogs.com/greta/p/5624839.html
使用退出標志,使線程正常退出,也就是當run方法完成后線程終止。 使用stop方法強行終止,但是不推薦這個方法,因為stop和suspend及resume一樣都是過期作廢的方法。 使用interrupt方法中斷線程。
# 停止不了的線程
public class MyThread extends Thread {public void run(){super.run();for(int i=0; i<500000; i++){System.out.println("i="+(i+1));}}}public class Run {public static void main(String args[]){Thread thread = new MyThread();thread.start();try {Thread.sleep(2000);thread.interrupt();} catch (InterruptedException e) {e.printStackTrace();}}}
...i=499994i=499995i=499996i=499997i=499998i=499999i=500000
# 判斷線程是否停止狀態(tài)
this.interrupted(): 測試當前線程是否已經(jīng)中斷; this.isInterrupted(): 測試線程是否已經(jīng)中斷;
public class MyThread extends Thread {public void run(){super.run();for(int i=0; i<500000; i++){i++;// System.out.println("i="+(i+1));}}}public class Run {public static void main(String args[]){Thread thread = new MyThread();thread.start();try {Thread.sleep(2000);thread.interrupt();System.out.println("stop 1??" + thread.interrupted());System.out.println("stop 2??" + thread.interrupted());} catch (InterruptedException e) {e.printStackTrace();}}}
stop 1??falsestop 2??false
System.out.println("stop 1??" + thread.interrupted());System.out.println("stop 2??" + thread.interrupted());
public class Run2 {public static void main(String args[]){Thread.currentThread().interrupt();System.out.println("stop 1??" + Thread.interrupted());System.out.println("stop 2??" + Thread.interrupted());System.out.println("End");}}
stop 1??truestop 2??falseEnd
public class Run3 {public static void main(String args[]){Thread thread = new MyThread();thread.start();thread.interrupt();System.out.println("stop 1??" + thread.isInterrupted());System.out.println("stop 2??" + thread.isInterrupted());}}
stop 1??truestop 2??true
# 能停止的線程--異常法
public class MyThread extends Thread {public void run(){super.run();for(int i=0; i<500000; i++){if(this.interrupted()) {System.out.println("線程已經(jīng)終止, for循環(huán)不再執(zhí)行");break;}System.out.println("i="+(i+1));}}}public class Run {public static void main(String args[]){Thread thread = new MyThread();thread.start();try {Thread.sleep(2000);thread.interrupt();} catch (InterruptedException e) {e.printStackTrace();}}}
...i=202053i=202054i=202055i=202056線程已經(jīng)終止, for循環(huán)不再執(zhí)行
public class MyThread extends Thread {public void run(){super.run();for(int i=0; i<500000; i++){if(this.interrupted()) {System.out.println("線程已經(jīng)終止, for循環(huán)不再執(zhí)行");break;}System.out.println("i="+(i+1));}System.out.println("這是for循環(huán)外面的語句,也會被執(zhí)行");}}
...i=180136i=180137i=180138i=180139線程已經(jīng)終止, for循環(huán)不再執(zhí)行這是for循環(huán)外面的語句,也會被執(zhí)行
public class MyThread extends Thread {public void run(){super.run();try {for(int i=0; i<500000; i++){if(this.interrupted()) {System.out.println("線程已經(jīng)終止, for循環(huán)不再執(zhí)行");throw new InterruptedException();}System.out.println("i="+(i+1));}System.out.println("這是for循環(huán)外面的語句,也會被執(zhí)行");} catch (InterruptedException e) {System.out.println("進入MyThread.java類中的catch了。。。");e.printStackTrace();}}}
...i=203798i=203799i=203800線程已經(jīng)終止, for循環(huán)不再執(zhí)行進入MyThread.java類中的catch了。。。java.lang.InterruptedExceptionat thread.MyThread.run(MyThread.java:13)
# 在沉睡中停止
public class MyThread extends Thread {public void run(){super.run();try {System.out.println("線程開始。。。");Thread.sleep(200000);System.out.println("線程結(jié)束。");} catch (InterruptedException e) {System.out.println("在沉睡中被停止, 進入catch, 調(diào)用isInterrupted()方法的結(jié)果是:" + this.isInterrupted());e.printStackTrace();}}}
線程開始。。。在沉睡中被停止, 進入catch, 調(diào)用isInterrupted()方法的結(jié)果是:falsejava.lang.InterruptedException: sleep interruptedat java.lang.Thread.sleep(Native Method)at thread.MyThread.run(MyThread.java:12)
public class MyThread extends Thread {public void run(){super.run();try {System.out.println("線程開始。。。");for(int i=0; i<10000; i++){System.out.println("i=" + i);}Thread.sleep(200000);System.out.println("線程結(jié)束。");} catch (InterruptedException e) {System.out.println("先停止,再遇到sleep,進入catch異常");e.printStackTrace();}}}public class Run {public static void main(String args[]){Thread thread = new MyThread();thread.start();thread.interrupt();}}
i=9998i=9999先停止,再遇到sleep,進入catch異常java.lang.InterruptedException: sleep interruptedat java.lang.Thread.sleep(Native Method)at thread.MyThread.run(MyThread.java:15)
# 能停止的線程---暴力停止
public class MyThread extends Thread {private int i = 0;public void run(){super.run();try {while (true){System.out.println("i=" + i);i++;Thread.sleep(200);}} catch (InterruptedException e) {e.printStackTrace();}}}public class Run {public static void main(String args[]) throws InterruptedException {Thread thread = new MyThread();thread.start();Thread.sleep(2000);thread.stop();}}
i=0i=1i=2i=3i=4i=5i=6i=7i=8i=9Process finished with exit code 0
# 方法stop()與java.lang.ThreadDeath異常
public class MyThread extends Thread {private int i = 0;public void run(){super.run();try {this.stop();} catch (ThreadDeath e) {System.out.println("進入異常catch");e.printStackTrace();}}}public class Run {public static void main(String args[]) throws InterruptedException {Thread thread = new MyThread();thread.start();}}
# 釋放鎖的不良后果
public class SynchronizedObject {private String name = "a";private String password = "aa";public synchronized void printString(String name, String password){try {this.name = name;Thread.sleep(100000);this.password = password;} catch (InterruptedException e) {e.printStackTrace();}}public String getName() {return name;}public void setName(String name) {this.name = name;}public String getPassword() {return password;}public void setPassword(String password) {this.password = password;}}public class MyThread extends Thread {private SynchronizedObject synchronizedObject;public MyThread(SynchronizedObject synchronizedObject){this.synchronizedObject = synchronizedObject;}public void run(){synchronizedObject.printString("b", "bb");}}public class Run {public static void main(String args[]) throws InterruptedException {SynchronizedObject synchronizedObject = new SynchronizedObject();Thread thread = new MyThread(synchronizedObject);thread.start();Thread.sleep(500);thread.stop();System.out.println(synchronizedObject.getName() + " " + synchronizedObject.getPassword());}}
b aa
# 使用return停止線程
public class MyThread extends Thread {public void run(){while (true){if(this.isInterrupted()){System.out.println("線程被停止了!");return;}System.out.println("Time: " + System.currentTimeMillis());}}}public class Run {public static void main(String args[]) throws InterruptedException {Thread thread = new MyThread();thread.start();Thread.sleep(2000);thread.interrupt();}}
...Time: 1467072288503Time: 1467072288503Time: 1467072288503線程被停止了!
最近面試BAT,整理一份面試資料《Java面試BATJ通關(guān)手冊》,覆蓋了Java核心技術(shù)、JVM、Java并發(fā)、SSM、微服務(wù)、數(shù)據(jù)庫、數(shù)據(jù)結(jié)構(gòu)等等。
朕已閱 
評論
圖片
表情

