今天腾讯一面中遇到这个问题---停止一个正在运行的线程?
下文笔者讲述今天面试中遇到的“停止一个正在运行的线程”的方法分享,如下所示
例:线程终止,但for循环还继续运行
但是通常情况下,此异常不需要显示地捕捉。
停止正在运行的线程简介
停止正在运行的线程:
程序的任务还未运行完毕,则终止操作
停止正在运行的线程有很多方法:
如:
1. Thread.stop()停止线程,
此方法不安全,已废弃
2.使用退出标志
使线程正常退出,run方法完成后线程终止
3.使用stop方法强行终止
由于stop和suspend及resume一样都是过期作废的方法
4.使用interrupt方法中断线程
interrupt()方法
interrupt()方法:
并不像for+break语句那样
马上就停止循环
调用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=499994
i=499995
i=499996
i=499997
i=499998
i=499999
i=500000
判断线程是否停止状态
Thread.java类中提供了两种方法:
this.interrupted():
测试当前线程是否已经中断
this.isInterrupted():
测试线程是否已经中断
能停止的线程--异常法
线程停止后,代码终止运行例:线程终止,但for循环还继续运行
public class MyThread extends Thread {
public void run(){
super.run();
for(int i=0; i<500000; i++){
if(this.interrupted()) {
System.out.println("线程已经终止, for循环不再执行");
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=202053
i=202054
i=202055
i=202056
线程已经终止, for循环不再执行
异常法终止线程
public class MyThread extends Thread {
public void run(){
super.run();
try {
for(int i=0; i<500000; i++){
if(this.interrupted()) {
System.out.println("线程已经终止, for循环不再执行");
throw new InterruptedException();
}
System.out.println("i="+(i+1));
}
System.out.println("这是for循环外面的语句,也会被执行");
} catch (InterruptedException e) {
System.out.println("进入MyThread.java类中的catch了。。。");
e.printStackTrace();
}
}
}
使用Run.java运行的结果如下:
...
i=203798
i=203799
i=203800
线程已经终止, for循环不再执行
进入MyThread.java类中的catch了。。。
java.lang.InterruptedException
at thread.MyThread.run(MyThread.java:13)
线程沉睡中停止
当线程在sleep()状态下停止线程
public class MyThread extends Thread {
public void run(){
super.run();
try {
System.out.println("线程开始。。。");
Thread.sleep(200000);
System.out.println("线程结束。");
} catch (InterruptedException e) {
System.out.println("在沉睡中被停止, 进入catch, 调用isInterrupted()方法的结果是:" + this.isInterrupted());
e.printStackTrace();
}
}
}
----运行以上代码,将输出以下信息-----
线程开始。。。
在沉睡中被停止, 进入catch, 调用isInterrupted()方法的结果是:false
java.lang.InterruptedException: sleep interrupted
at java.lang.Thread.sleep(Native Method)
at thread.MyThread.run(MyThread.java:12)
stop方法暴力停止线程
使用stop()方法暴力停止线程
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=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
方法stop()与java.lang.ThreadDeath异常
调用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();
}
}
使用return停止线程
使用方法interrupt()与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();
}
}
笔者建议使用异常法终止线程 并将异常向上抛
版权声明
本文仅代表作者观点,不代表本站立场。
本文系作者授权发表,未经许可,不得转载。


