如何停止一个正在运行的线程

总工有三种方式可停止当前正在运行的线程:

  1. 使用退出标志,使线程正常退出,也就是当run方法完成后线程终止;
  2. 使用interrupt方法中断线程 ;
  3. 使用stop方法强行终止(不推荐使用该方法,目前该方法已作废)。

 (1)使用退出标志,使线程正常退出,也就是当run方法完成后线程终止

代码示例:

public class MyThread extends Thread {
volatile boolean flag = false ; // 线程执行的退出标记
@Override
public void run() {
while(!flag) {
System.out.println("MyThread...run...");
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public static void main(String[] args) throws
InterruptedException {
// 创建MyThread对象
MyThread t1 = new MyThread() ;
t1.start();
// 主线程休眠6秒
Thread.sleep(6000);
// 更改标记为true
t1.flag = true ;
}
}

输出效果: 

MyThread...run...

MyThread...run...

 (2)使用interrupt方法中断线程

代码示例:

package com.company.test;

public class MyThread extends Thread {
    // 线程执行的退出标记
    @Override
    public void run() {

        while (!Thread.currentThread().isInterrupted()) {
            System.out.println(Thread.currentThread().isInterrupted());
            System.out.println("MyThread...run...");
        }
        System.out.println(Thread.currentThread().isInterrupted());
    }

    public static void main(String[] args) throws InterruptedException {
        // 创建MyThread对象
        MyThread t1 = new MyThread();
        t1.start();
        // 主线程休眠2秒
        Thread.sleep(3000);
        // 调用interrupt方法
        t1.interrupt();//并不能直接中断对应子线程;之线程无法感知;
    }
}

输出效果:

MyThread...run...
false
.....................
false
MyThread...run...
false
MyThread...run...
true

注意!!!

如果在子线程中用Thread.sleep(),需要对其进行异常捕获,然而捕获以后,线程的中断(讯息)将会被捕获且清除,从而子线程无法感知上层方法中--实例线程对象调用interrupt()对线程的中断,即
Thread.currentThread().isInterrupted()

显示结果为false;线程将一直运行。

示例代码:

public class MyThread extends Thread {
    @Override
    public void run() {
        while(!Thread.currentThread().isInterrupted()) {
            System.out.println(Thread.currentThread().isInterrupted());
            System.out.println("MyThread...run...");
            try {
                Thread.sleep(3000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        System.out.println(Thread.currentThread().isInterrupted());
        }
    public static void main(String[] args) throws
            InterruptedException {
        // 创建MyThread对象
        MyThread t1 = new MyThread() ;
        t1.start();
        // 主线程休眠2秒
        Thread.sleep(3000);
        // 调用interrupt方法,对子线程进行中断
        t1.interrupt();
    }
}

显示结果:

false
MyThread...run...
false
MyThread...run...
false
MyThread...run...
java.lang.InterruptedException: sleep interrupted
    at java.lang.Thread.sleep(Native Method)
    at com.company.test.MyThread.run(MyThread.java:10)
false
MyThread...run...
false
MyThread...run...
........

........

 可见,在子线程中使用异常捕获之后,线程将无法感知上层方法对它的中断。

但非要让子线程感知中断,只能通过手动调用的方式:

修改后的代码:


public class MyThread extends Thread {
    @Override
    public void run() {
        while(!Thread.currentThread().isInterrupted()) {
            System.out.println(Thread.currentThread().isInterrupted());
            System.out.println("MyThread...run...");
            try {
                Thread.sleep(3000);
            } catch (InterruptedException e) {
                e.printStackTrace();
                /**
                 * 手动调用恢复中断
                 */
                Thread.currentThread().interrupt();
                System.out.println(Thread.currentThread().isInterrupted());
            }
        }
        System.out.println(Thread.currentThread().isInterrupted());
        }
    public static void main(String[] args) throws
            InterruptedException {
        // 创建MyThread对象
        MyThread t1 = new MyThread() ;
        t1.start();
        // 主线程休眠2秒
        Thread.sleep(3000);
        // 调用interrupt方法,对子线程进行中断
        t1.interrupt();
    }
}

显示结果:

false
MyThread...run...
false
MyThread...run...
true
true
java.lang.InterruptedException: sleep interrupted
    at java.lang.Thread.sleep(Native Method)
    at com.company.test.MyThread.run(MyThread.java:10)

Process finished with exit code 0

可见,通过手动调用后,线程执行可以被中断。

(3)使用stop方法强行终止

public class MyThread extends Thread {
volatile boolean flag = false ; // 线程执行的退出标记
@Override
public void run() {
while(!flag) {
System.out.println("MyThread...run...");
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public static void main(String[] args) throws
InterruptedException {
// 创建MyThread对象
MyThread t1 = new MyThread() ;
t1.start();
// 主线程休眠2秒
Thread.sleep(6000);
// 调用stop方法
t1.stop();
}
}

现在编码中,通过stop强行停止线程的方法已经被废弃。

  • 2
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值