6、停止執行緒
阿新 • • 發佈:2018-12-22
如何判斷執行緒是否是停止狀態
- this.interrupted(); 測試當前執行緒是否已經中斷
- this.isInterrupted(); 測試執行緒是否已經中斷
對於this.interrupted(); 測試當前執行緒是否已經中斷 進行實驗如下:
package com.com.demo7; public class MyThread extends Thread { @Override public void run() { for (int i = 0; i < 500000; i++) { System.out.println("i=" + i); } } }
package com.com.demo7; public class Run { public static void main(String[] args) { try { MyThread myThread = new MyThread(); myThread.start(); Thread.sleep(8000); myThread.interrupt(); System.out.println("是否停止?" + myThread.interrupted()); } catch (InterruptedException e) { e.printStackTrace(); } } }
執行結果:
...
i=499997
i=499998
i=499999
是否停止?false
將Run.java進行修改 如下:
package com.com.demo7; public class Run { public static void main(String[] args) { try { MyThread myThread = new MyThread(); myThread.start(); Thread.sleep(8000); myThread.interrupt(); Thread.currentThread().interrupt(); System.out.println("是否停止?" + myThread.interrupted()); } catch (InterruptedException e) { e.printStackTrace(); } } }
執行結果:
...
i=499996
i=499997
i=499998
i=499999
是否停止?true
修改Run.java如下:
package com.com.demo7;
public class Run {
public static void main(String[] args) {
try {
MyThread myThread = new MyThread();
myThread.start();
Thread.sleep(8000);
myThread.interrupt();
Thread.currentThread().interrupt();
//System.out.println("是否停止?" + myThread.interrupted());
System.out.println("是否停止?" + Thread.interrupted());
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
執行結果:
...
i=499997
i=499998
i=499999
是否停止?true
分析:主執行緒”睡“8秒鐘才執行列印語句,這期間執行緒,執行緒myThread已經執行結束,但是輸出結果是false。足以驗證 interrupted()測試的是當前執行緒是否停止,而main是當前執行緒。一直沒有終止。所以結果是false。當呼叫Thread.currentThread().interrupt()之後,主執行緒main(當前執行緒)停止,列印true
通過interrupted()狀態,採用拋異常的方式來終止執行緒
package com.com.demo7;
public class MyThread extends Thread {
@Override
public void run() {
try {
for (int i = 0; i < 500000; i++) {
if(this.interrupted()){
System.out.println("本執行緒是終止狀態");
//break;
throw new InterruptedException();
}
System.out.println("i=" + i);
}
System.out.println("雖然for被break,但執行緒一直在進行");
} catch (InterruptedException e) {
System.out.println("捕獲異常,禁止上面程式碼段中for語句下面的業務繼續進行");
e.printStackTrace();
}
}
}
package com.com.demo7;
public class Run {
public static void main(String[] args) {
try {
MyThread myThread = new MyThread();
myThread.start();
Thread.sleep(2000);
myThread.interrupt();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
執行結果:
i=108917
i=108918
本執行緒是終止狀態
捕獲異常,禁止上面程式碼段中for語句下面的業務繼續進行
java.lang.InterruptedException
at com.com.demo7.MyThread.run(MyThread.java:11)
通過sleep()方法停止執行緒
package com.com.demo8;
public class MyThread extends Thread {
@Override
public void run() {
try {
System.out.println("run begin");
Thread.sleep(200000);
System.out.println("run end");
} catch (InterruptedException e) {
System.out.println("在sleep()中被停止,進入catch() : " + this.isInterrupted());
e.printStackTrace();
}
}
}
package com.com.demo8;
public class Run {
public static void main(String[] args) {
try {
MyThread myThread = new MyThread();
myThread.start();
Thread.sleep(1000);
myThread.interrupt();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
執行結果:
run begin
在sleep()中被停止,進入catch() : false
java.lang.InterruptedException: sleep interrupted
at java.lang.Thread.sleep(Native Method)
at com.com.demo8.MyThread.run(MyThread.java:8)
使用stop()來暴力停止執行緒
package com.com.demo9;
public class MyThread extends Thread {
private int i = 0;
@Override
public void run() {
try {
while (true) {
i++;
System.out.println("i=" + i);
Thread.sleep(1000);
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
package com.com.demo9;
public class Run {
public static void main(String[] args) {
try {
MyThread myThread = new MyThread();
myThread.start();
Thread.sleep(5000);
myThread.stop();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
執行結果:
i=1
i=2
i=3
i=4
i=5
通過stop()方法來停止執行緒,可能造成資料不一致的問題。(已經被棄用)
package com.com.demo10;
public class MyObject {
private String username = "a";
private String password = "aa";
public String getUsername() {
return username;
}
public String getPassword() {
return password;
}
synchronized public void printString(String username , String password){
try {
this.username = username;
Thread.sleep(20000);
this.password = password;
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
package com.com.demo10;
public class MyThread extends Thread {
private MyObject myObject = new MyObject();
public MyThread(MyObject myObject) {
this.myObject = myObject;
}
@Override
public void run() {
myObject.printString("b","bb");
}
}
package com.com.demo10;
public class Run {
public static void main(String[] args) {
try {
MyObject myObject = new MyObject();
MyThread myThread = new MyThread(myObject);
myThread.start();
Thread.sleep(500);
myThread.stop();
System.out.println("username = " + myObject.getUsername() +
" ; password = " + myObject.getPassword());
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
執行結果:
username = b ; password = aa
使用return來停止執行緒
package com.com.demo11;
import jdk.nashorn.internal.ir.WhileNode;
import java.text.SimpleDateFormat;
public class MyThread extends Thread{
private SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
@Override
public void run() {
while (true) {
if (this.isInterrupted()){
System.out.println("停止了");
return;
}
System.out.println("timer:" + sdf.format(System.currentTimeMillis()));
}
}
}
package com.com.demo11;
public class Run {
public static void main(String[] args) {
try {
MyThread myThread = new MyThread();
myThread.start();
Thread.sleep(2000);
myThread.interrupt();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
執行結果:
...
timer:2018-12-04 22:32:22
停止了