騰訊面試官:如何停止一個(gè)正在運(yùn)行的線(xiàn)程?我一臉蒙蔽。。。
https://www.cnblogs.com/greta/p/5624839.html
停止一個(gè)線(xiàn)程意味著在任務(wù)處理完任務(wù)之前停掉正在做的操作,也就是放棄當(dāng)前的操作。停止一個(gè)線(xiàn)程可以用 Thread.stop() 方法,但最好不要用它。雖然它確實(shí)可以停止一個(gè)正在運(yùn)行的線(xiàn)程,但是這個(gè)方法是不安全的,而且是已被廢棄的方法。
在 java 中有以下 3 種方法可以終止正在運(yùn)行的線(xiàn)程:
使用退出標(biāo)志,使線(xiàn)程正常退出,也就是當(dāng) run 方法完成后線(xiàn)程終止。
使用 stop 方法強(qiáng)行終止,但是不推薦這個(gè)方法,因?yàn)?stop 和 suspend 及 resume 一樣都是過(guò)期作廢的方法。
使用 interrupt 方法中斷線(xiàn)程。
1. 停止不了的線(xiàn)程
interrupt() 方法的使用效果并不像 for+break 語(yǔ)句那樣,馬上就停止循環(huán)。調(diào)用 interrupt 方法是在當(dāng)前線(xiàn)程中打了一個(gè)停止標(biāo)志,并不是真的停止線(xiàn)程。
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();
}
}
}
輸出結(jié)果:
...
i=499994
i=499995
i=499996
i=499997
i=499998
i=499999
i=500000
2. 判斷線(xiàn)程是否停止?fàn)顟B(tài)
Thread.java 類(lèi)中提供了兩種方法:
this.interrupted(): 測(cè)試當(dāng)前線(xiàn)程是否已經(jīng)中斷;
this.isInterrupted(): 測(cè)試線(xiàn)程是否已經(jīng)中斷;
那么這兩個(gè)方法有什么圖區(qū)別呢?
我們先來(lái)看看 this.interrupted() 方法的解釋?zhuān)簻y(cè)試當(dāng)前線(xiàn)程是否已經(jīng)中斷,當(dāng)前線(xiàn)程是指運(yùn)行 this.interrupted() 方法的線(xiàn)程。
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();
}
}
}
運(yùn)行結(jié)果:
stop 1??false
stop 2??false
類(lèi) Run.java 中雖然是在 thread 對(duì)象上調(diào)用以下代碼:thread.interrupt(), 后面又使用
System.out.println("stop 1??" + thread.interrupted());
System.out.println("stop 2??" + thread.interrupted());
來(lái)判斷 thread 對(duì)象所代表的線(xiàn)程是否停止,但從控制臺(tái)打印的結(jié)果來(lái)看,線(xiàn)程并未停止,這也證明了 interrupted() 方法的解釋?zhuān)瑴y(cè)試當(dāng)前線(xiàn)程是否已經(jīng)中斷。這個(gè)當(dāng)前線(xiàn)程是 main,它從未中斷過(guò),所以打印的結(jié)果是兩個(gè) false.
如何使 main 線(xiàn)程產(chǎn)生中斷效果呢?
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");
}
}
運(yùn)行效果為:
stop 1??true
stop 2??false
End
方法 interrupted() 的確判斷出當(dāng)前線(xiàn)程是否是停止?fàn)顟B(tài)。但為什么第 2 個(gè)布爾值是 false 呢?官方幫助文檔中對(duì) interrupted 方法的解釋?zhuān)?br>測(cè)試當(dāng)前線(xiàn)程是否已經(jīng)中斷。線(xiàn)程的中斷狀態(tài)由該方法清除。 換句話(huà)說(shuō),如果連續(xù)兩次調(diào)用該方法,則第二次調(diào)用返回 false。
下面來(lái)看一下 inInterrupted() 方法。
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());
}
}
運(yùn)行結(jié)果:
stop 1??true
stop 2??true
isInterrupted() 并為清除狀態(tài),所以打印了兩個(gè) true。
3. 能停止的線(xiàn)程 -- 異常法
有了前面學(xué)習(xí)過(guò)的知識(shí)點(diǎn),就可以在線(xiàn)程中用 for 語(yǔ)句來(lái)判斷一下線(xiàn)程是否是停止?fàn)顟B(tài),如果是停止?fàn)顟B(tài),則后面的代碼不再運(yùn)行即可:
public class MyThread extends Thread {
public void run(){
super.run();
for(int i=0; i<500000; i++){
if(this.interrupted()) {
System.out.println("線(xiàn)程已經(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();
}
}
}
運(yùn)行結(jié)果:
...
i=202053
i=202054
i=202055
i=202056
線(xiàn)程已經(jīng)終止, for循環(huán)不再執(zhí)行
上面的示例雖然停止了線(xiàn)程,但如果 for 語(yǔ)句下面還有語(yǔ)句,還是會(huì)繼續(xù)運(yùn)行的。看下面的例子:
public class MyThread extends Thread {
public void run(){
super.run();
for(int i=0; i<500000; i++){
if(this.interrupted()) {
System.out.println("線(xiàn)程已經(jīng)終止, for循環(huán)不再執(zhí)行");
break;
}
System.out.println("i="+(i+1));
}
System.out.println("這是for循環(huán)外面的語(yǔ)句,也會(huì)被執(zhí)行");
}
}
使用 Run.java 執(zhí)行的結(jié)果是:
...
i=180136
i=180137
i=180138
i=180139
線(xiàn)程已經(jīng)終止, for循環(huán)不再執(zhí)行
這是for循環(huán)外面的語(yǔ)句,也會(huì)被執(zhí)行
如何解決語(yǔ)句繼續(xù)運(yùn)行的問(wèn)題呢?看一下更新后的代碼:
public class MyThread extends Thread {
public void run(){
super.run();
try {
for(int i=0; i<500000; i++){
if(this.interrupted()) {
System.out.println("線(xiàn)程已經(jīng)終止, for循環(huán)不再執(zhí)行");
throw new InterruptedException();
}
System.out.println("i="+(i+1));
}
System.out.println("這是for循環(huán)外面的語(yǔ)句,也會(huì)被執(zhí)行");
} catch (InterruptedException e) {
System.out.println("進(jìn)入MyThread.java類(lèi)中的catch了。。。");
e.printStackTrace();
}
}
}
使用 Run.java 運(yùn)行的結(jié)果如下:
...
i=203798
i=203799
i=203800
線(xiàn)程已經(jīng)終止, for循環(huán)不再執(zhí)行
進(jìn)入MyThread.java類(lèi)中的catch了。。。
java.lang.InterruptedException
at thread.MyThread.run(MyThread.java:13)
4. 在沉睡中停止
如果線(xiàn)程在 sleep() 狀態(tài)下停止線(xiàn)程,會(huì)是什么效果呢?
public class MyThread extends Thread {
public void run(){
super.run();
try {
System.out.println("線(xiàn)程開(kāi)始。。。");
Thread.sleep(200000);
System.out.println("線(xiàn)程結(jié)束。");
} catch (InterruptedException e) {
System.out.println("在沉睡中被停止, 進(jìn)入catch, 調(diào)用isInterrupted()方法的結(jié)果是:" + this.isInterrupted());
e.printStackTrace();
}
}
}
使用 Run.java 運(yùn)行的結(jié)果是:
線(xiàn)程開(kāi)始。。。
在沉睡中被停止, 進(jìn)入catch, 調(diào)用isInterrupted()方法的結(jié)果是:false
java.lang.InterruptedException: sleep interrupted
at java.lang.Thread.sleep(Native Method)
at thread.MyThread.run(MyThread.java:12)
從打印的結(jié)果來(lái)看, 如果在 sleep 狀態(tài)下停止某一線(xiàn)程,會(huì)進(jìn)入 catch 語(yǔ)句,并且清除停止?fàn)顟B(tài)值,使之變?yōu)?false。
前一個(gè)實(shí)驗(yàn)是先 sleep 然后再用 interrupt() 停止,與之相反的操作在學(xué)習(xí)過(guò)程中也要注意:
public class MyThread extends Thread {
public void run(){
super.run();
try {
System.out.println("線(xiàn)程開(kāi)始。。。");
for(int i=0; i<10000; i++){
System.out.println("i=" + i);
}
Thread.sleep(200000);
System.out.println("線(xiàn)程結(jié)束。");
} catch (InterruptedException e) {
System.out.println("先停止,再遇到sleep,進(jìn)入catch異常");
e.printStackTrace();
}
}
}
public class Run {
public static void main(String args[]){
Thread thread = new MyThread();
thread.start();
thread.interrupt();
}
}
運(yùn)行結(jié)果:
i=9998
i=9999
先停止,再遇到sleep,進(jìn)入catch異常
java.lang.InterruptedException: sleep interrupted
at java.lang.Thread.sleep(Native Method)
at thread.MyThread.run(MyThread.java:15)
5. 能停止的線(xiàn)程 --- 暴力停止
使用 stop() 方法停止線(xiàn)程則是非常暴力的。
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();
}
}
運(yùn)行結(jié)果:
i=0
i=1
i=2
i=3
i=4
i=5
i=6
i=7
i=8
i=9
Process finished with exit code 0
6. 方法 stop() 與 java.lang.ThreadDeath 異常
調(diào)用 stop() 方法時(shí)會(huì)拋出 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("進(jìn)入異常catch");
e.printStackTrace();
}
}
}
public class Run {
public static void main(String args[]) throws InterruptedException {
Thread thread = new MyThread();
thread.start();
}
}
stop() 方法以及作廢,因?yàn)槿绻麖?qiáng)制讓線(xiàn)程停止有可能使一些清理性的工作得不到完成。另外一個(gè)情況就是對(duì)鎖定的對(duì)象進(jìn)行了解鎖,導(dǎo)致數(shù)據(jù)得不到同步的處理,出現(xiàn)數(shù)據(jù)不一致的問(wèn)題。
7. 釋放鎖的不良后果
使用 stop() 釋放鎖將會(huì)給數(shù)據(jù)造成不一致性的結(jié)果。如果出現(xiàn)這樣的情況,程序處理的數(shù)據(jù)就有可能遭到破壞,最終導(dǎo)致程序執(zhí)行的流程錯(cuò)誤,一定要特別注意:
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());
}
}
輸出結(jié)果:
b aa
由于 stop()方法以及在 JDK 中被標(biāo)明為 “過(guò)期 / 作廢” 的方法,顯然它在功能上具有缺陷,所以不建議在程序張使用 stop()方法。
8. 使用 return 停止線(xiàn)程
將方法 interrupt() 與 return 結(jié)合使用也能實(shí)現(xiàn)停止線(xiàn)程的效果:
public class MyThread extends Thread {
public void run(){
while (true){
if(this.isInterrupted()){
System.out.println("線(xiàn)程被停止了!");
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();
}
}
輸出結(jié)果:
...
Time: 1467072288503
Time: 1467072288503
Time: 1467072288503
線(xiàn)程被停止了!
不過(guò)還是建議使用 “拋異?!?的方法來(lái)實(shí)現(xiàn)線(xiàn)程的停止,因?yàn)樵?catch 塊中還可以將異常向上拋,使線(xiàn)程停止事件得以傳播。
最近給大家找了 Vue進(jìn)階
資源,怎么領(lǐng)???
掃二維碼,加我微信,回復(fù):Vue進(jìn)階
注意,不要亂回復(fù) 沒(méi)錯(cuò),不是機(jī)器人 記得一定要等待,等待才有好東西
