线程停止的三种方式超详细

线程停止的几种方式

1、简单粗暴法Stop

此方法已经废弃,不建议使用,jdk帮助文档有如下解释
在这里插入图片描述

1.1、从代码执行结果层面解释

package com.study.test;

public class ThreadStopTest {
    public static void main(String[] args) throws InterruptedException {
        StopTest stopTest = new StopTest();
        stopTest.start();
        Thread.sleep(100);
        stopTest.stop();
    }
}

class StopTest extends Thread{
    @Override
    public void run() {
        try {
            long start=System.currentTimeMillis();
            //开始计数
            for (int i = 0; i < 100000; i++){
                System.out.println("runing.." + i);
                Thread.sleep(10);
            }
            System.out.println((System.currentTimeMillis()-start)+"ms");
        } catch (Throwable ex) {
            System.out.println("Caught in run: " + ex);
            ex.printStackTrace();
        } finally {
            System.out.println("run finally");
        }
    }
}

执行结果:抛出异常ThreadDeath(也说明了stop方法执行会暴力去结束线程,不管线程上的任务是否结束)

在这里插入图片描述

1.2、从抢占锁的角度考虑

package com.study.test;

import java.util.concurrent.TimeUnit;

public class Test {
    public static void main(String[] args) throws InterruptedException {
        //定义锁对象
        final Object lock = new Object();
        // 先让线程t1持有锁,并且等待3秒然后释放
        Thread t1 = new Thread(()->{
            try {
                synchronized (lock) {
                    System.out.println(Thread.currentThread().getName() + " 拿到锁");
                    TimeUnit.SECONDS.sleep(3);
                    System.out.println(Thread.currentThread().getName() + " 持有3s锁~逻辑处理中");
                    System.out.println(Thread.currentThread().getName() + " 释放锁");
                }
            } catch (Throwable ex) {
                ex.printStackTrace();
            }
        });

        // 线程2去排队等待锁
        Thread t2 = new Thread(()->{
            synchronized (lock) {
                System.out.println(Thread.currentThread().getName()  + " 拿到锁");
            }
        });

        // 拿锁持有3秒
        t1.start();
        Thread.sleep(100);
        t1.stop();
        t2.start();
    }
}

执行结果:线程1本来应该持有锁三秒,但是因为调用线程1的stop方法马上释放掉锁

在这里插入图片描述

正常结果应该如下(注释掉t1.stop();逻辑代码)
在这里插入图片描述

1.3、总结

  • 从执行代码结果的层面来看如果直接调用stop方法会即刻在线程的run()方法内抛出ThreadDeath异常,而且抛出的位置不确定,还有可能在catch或finally语句中。
  • 并且直接使用stop停止线程是非常不明智的,如果突然释放锁会导致本来加锁的对象被其他线程抢到锁从而修改属性值,导致数据不一致的情况产生。

2、标志位法

这种方法的好处是让线程自己停下来,实现方式就是将一个变量作为标识,线程每隔一段时间去判断下这个变量是否停止为停止标识,如果是则停止,否则继续运行

2.1、代码实现

package com.study.test;

import java.sql.Time;
import java.util.concurrent.TimeUnit;

/**
 * @author luosong
 * @version 1.0
 * @date 2021/2/24 12:44
 */
public class StopThread {
    public static void main(String[] args) throws InterruptedException {
        ThreadDemo threadDemo = new ThreadDemo();

        Thread thread = new Thread(threadDemo);
        thread.start();
        TimeUnit.SECONDS.sleep(3);
        threadDemo.stop();
    }
}

class ThreadDemo implements Runnable{
    public volatile boolean flag = true;
    @Override
    public void run() {
        while (flag){
            System.out.println("do... something");
        }
    }

    public void stop(){
        flag = false;
        System.out.println("run... stop");
    }
}

2.2、注意点

  • volatile关键字,在多线程的情况下改变变量的值,如果需要马上通知其他线程,那么可以使用这个关键字,这是基于volatile关键字的特性—>可见性。
  • 在正常开发中肯定是不会去使用死循环来去判断是否需要停止线程的,这个可以在关键业务逻辑中去判断

3、interrupt停止标记法

3.1、interrupt中断方法介绍

熟悉这种方法前必须先了解一组API

  • public void interrupt() 中断线程
  • public static boolean interrupted() 测试当前线程是否中断
  • public boolean isInterrupted() 测试当前线程是否中断

在这里插入图片描述

从API文档中可以比较清晰的看出,判断线程是否已经被中断有两个方法,interrupted在判断线程是否中断的同时还清除了线程的中断状态

3.2、代码解析中断判断方法

3.2.1、interrupted
public class StopThread2 {
    public static void main(String[] args) {
        // 中断线程
        Thread.currentThread().interrupt();
        // 测试线程是否是中断状态,执行后将中断标志更改为false
        // true
        System.out.println(Thread.interrupted());
        // false
        System.out.println(Thread.interrupted());
        // false
        System.out.println(Thread.interrupted());
    }
}
3.2.2、isInterrupted
public class StopThread2 {
    public static void main(String[] args) {
        // 中断线程
        Thread.currentThread().interrupt();
        // isInterrupted:测试线程是否是中断状态,执行后不更改状态标志
        // true
        System.out.println(Thread.currentThread().isInterrupted());
        // true
        System.out.println(Thread.currentThread().isInterrupted());
        // true
        System.out.println(Thread.currentThread().isInterrupted());
    }
}

3.3、中断程序如何实现

其实interrupt中断方法和标志位法原理相似,但标识变量不需要自己定义

package com.study.test;

/**
 * @author luosong
 * @version 1.0
 * @date 2021/2/24 22:40
 */
public class StopThread3 {
    public static void main(String[] args) throws InterruptedException {
        InterruptThread thread = new InterruptThread();

        thread.start();
        Thread.sleep(3000);
        thread.stop();
    }
}

class InterruptThread {
    public Thread thread;

    public void start(){
        thread = new Thread(()->{
            while (!thread.isInterrupted()){
                System.out.println("do...something");
            }
        });
        thread.start();
    }

    public void stop(){
        thread.interrupt();
    }
}

3.4、interrupt中断方法的特性

通过jdk文档可知,如果在线程阻塞时调用中断方法会抛出异常,同时清理中断状态

在这里插入图片描述

代码演示如下

/**
 * @author luosong
 * @version 1.0
 * @date 2021/2/24 22:40
 */
public class StopThread2 {
    public static void main(String[] args) throws InterruptedException {
        InterruptThread1 taskCase3 = new InterruptThread1();
        taskCase3.start();
        Thread.sleep(3000);
        taskCase3.stop();
    }
}

class InterruptThread1 {
    private Thread thread;

    public void start() {
        thread = new Thread(() -> {
            while (!Thread.currentThread().isInterrupted()) {
                try {
                    System.out.println("doSomething");
                    TimeUnit.MICROSECONDS.sleep(100);
                } catch (InterruptedException e) {
                    // 重置中断标志位为true
                    // thread.interrupt();
                    e.printStackTrace();
                    System.out.println("抛出中断异常,中断程序");
                }
            }
        });
        thread.start();
    }

    public void stop() {
        thread.interrupt();
    }
}

注意:从jdk文档可知,如果interrupt中断的是阻塞的线程,那么就会导致抛出异常InterruptedException,并且会清理中断标志位,所以如上代码是一个死循环,如何解决呢?就是要重置标识位thread.interrupt();,catch异常后要重置标识位即可解决问题

3.5、思考

现在可以了解到如果是线程阻塞然后中断就会抛出异常,那么假设线程先interrupt中断后再去阻塞会怎么样呢?

代码模拟

/**
 * @author luosong
 * @version 1.0
 * @date 2021/2/24 22:40
 */
public class StopThread4 {
    public static void main(String[] args) throws InterruptedException {
        Thread thread = new Thread(()->{
            try {
                // 数值尽可能的大
                for (int i = 0; i < 10000; i++) {
                    System.out.println( "i="  +(i + 1) );
                }
                System.out.println( "开始阻塞" );
                Thread.sleep( 200 );
                System.out.println( "阻塞完毕" );
            } catch (InterruptedException e) {
                System.out.println( "中断报错·······" );
                e.printStackTrace();
            }
        });

        thread.start();
        thread.interrupt();
    }
}

在这里插入图片描述

结论:如果线程先中断然后进入阻塞同样会抛出异常

4、总结

本文总共介绍三种停止线程的方法,stop暴力方法肯定不推荐使用而且方法已经过时,通过对标志位法以及interrupt的对比明显可以得出,interrupt使用更加的健全,提供了API可以供我们使用。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值