我们知道Thread.stop方法已经被标记为过时方法了,因为使用stop会有很大一些问题。那么除了stop方法,我们还能用以下方法来实现结束线程功能
1、维护一个线程是否结束的flag
2、使用interrupt
3、定义守护线程
维护一个线程是否结束的flag
package com.thread.demo;
import java.util.concurrent.TimeUnit;
/**
* @Author: Peacock__
* @Date: 2019/4/29 14:00
*/
public class StopThread1 implements Runnable {
//定义flag,默认false
private volatile boolean isFinished;
@Override
public void run() {
//不满足flag条件就一直执行
while(!isFinished){
System.out.println("go on");
}
}
public void shutoff(){
//置为已完成状态
this.isFinished = true;
}
public static void main(String[] args) throws InterruptedException {
StopThread1 stopThread1 = new StopThread1();
Thread t = new Thread(stopThread1);
t.start();
//休眠2秒
TimeUnit.SECONDS.sleep(2);
//维护flag
stopThread1.shutoff();
}
}
控制台输出结果是:连续2秒不断打出“go on”,2秒后程序自动退出。
使用interrupt
package com.thread.demo;
import java.util.concurrent.TimeUnit;
/**
* @Author: Peacock__
* @Date: 2019/4/29 14:00
*/
public class StopThread2 implements Runnable {
@Override
public void run() {
//没有被打断就一直执行
while(!Thread.interrupted()){
System.out.println("go on");
}
}
public static void main(String[] args) throws InterruptedException {
StopThread2 stopThread1 = new StopThread2();
Thread t = new Thread(stopThread1);
t.start();
//休眠2秒
TimeUnit.SECONDS.sleep(2);
//打断
t.interrupt();
}
}
控制台输出结果是:连续2秒不断打出“go on”,2秒后程序自动退出。
定义守护线程
package com.thread.demo;
import java.util.concurrent.TimeUnit;
/**
* @Author: Peacock__
* @Date: 2019/4/29 14:00
*/
public class StopThread3 {
private Thread thread;
private boolean isFinished;
public void execute(Runnable task){
thread = new Thread() {
@Override
public void run() {
//在线程里创建新的子线程
Thread run = new Thread(task);
//设置为守护线程
run.setDaemon(true);
run.start();
try {
//让thread一直等到run执行完毕再向下执行
run.join();
isFinished = true;
} catch (InterruptedException e) {
}
}
};
thread.start();
}
public void shutoff(long mills){
//获取当前毫秒数
long currentTimeMillis = System.currentTimeMillis();
//不满足已经完成状态就一致刷
while (!isFinished){
//当前时间 - 开始执行的时间 > 可等待的时间
if((System.currentTimeMillis() - currentTimeMillis) > mills){
System.out.println("超时");
//中断
thread.interrupt();
isFinished = true;
break;
}else{
//当前时间 - 开始执行的时间 <= 可等待的时间的情况,就小小的休眠一会
try {
TimeUnit.SECONDS.sleep(1);
} catch (InterruptedException e) {
System.out.println("程序被打断");
}
}
}
}
}
package com.thread.demo;
/**
* @Author: Peacock__
* @Date: 2019/4/29 14:49
*/
public class StopThreadTest {
public static void main(String[] args) {
StopThread3 stopThread3 = new StopThread3();
long start = System.currentTimeMillis();
stopThread3.execute(() -> {
//模拟线程执行需要时间,远超10秒
while(true){
}
});
//设置最多等待10秒
stopThread3.shutoff(10_000);
long end = System.currentTimeMillis();
System.out.println(end-start);
}
}
控制台输出
可以看出,当超出10秒的时候,线程会被中断,程序退出。
因为我们在shutoff方法里有灵活的判断,和isFinished标识,所以当任务只需5秒就能执行完毕的时候,我们的程序5秒就会退出,不会傻傻的等待5秒(((o(*゚▽゚*)o)))
package com.thread.demo;
/**
* @Author: Peacock__
* @Date: 2019/4/29 14:49
*/
public class StopThreadTest {
public static void main(String[] args) {
StopThread3 stopThread3 = new StopThread3();
long start = System.currentTimeMillis();
stopThread3.execute(() -> {
//模拟线程执行需要时间,远超10秒
try {
Thread.sleep(5_000);
} catch (InterruptedException e) {
e.printStackTrace();
}
});
//设置最多等待10秒
stopThread3.shutoff(10_000);
long end = System.currentTimeMillis();
System.out.println(end-start);
}
}