美文网首页
线程终止,中断

线程终止,中断

作者: 小飞剑客 | 来源:发表于2021-02-06 10:09 被阅读0次

线程中断有两种方式
1、interrrupt stop
interrupt 标记当前线程的状态为中断,stop粗暴强制中断当前线程。

Thread thread = new Thread();
        thread.stop();
        thread.interrupt();

首先看看interrupt中断线程比如:


public class ThreadInterrupt extends Thread{

    public static void main(String[] args) throws InterruptedException {
        ThreadInterrupt threadInterrupt = new ThreadInterrupt();
        threadInterrupt.start();
        sleep(300);
        threadInterrupt.interrupt();
    }

    @Override
    public void run() {
        for (int i = 0; i < 200000; i++) {
            System.out.println("输出为:"+(i+1))
        }
    }
}

interrupt 中断线程是把线程标记为中断,后续处理交给线程自己,他会循环所有。
输出为:199995
输出为:199996
输出为:199997
输出为:199998
输出为:199999
输出为:200000

Thread thread = new Thread();
        thread.stop();
        thread.interrupt();

再看看stop() 来中断线程比如:


public class ThreadInterrupt extends Thread{

    public static void main(String[] args) throws InterruptedException {
        ThreadInterrupt threadInterrupt = new ThreadInterrupt();
        threadInterrupt.start();
        sleep(300);
        threadInterrupt.stop();
    }

    @Override
    public void run() {
        for (int i = 0; i < 200000; i++) {
            System.out.println("输出为:"+(i+1))
        }
    }
}

sopt 中断线程是直接终止线程内的所有逻辑,全部停止。
输出为:53210
输出为:53211
输出为:53212
输出为:53213
输出为:53214

2、中断状态标记,首先解释三个名词
interrupt 中断当前线程
isInterrupted 判断当前线程状态是否被中断 true:已经被中断, false: 未被中断。
interrupted 判断当前线程是否被中断 true:已被中断, false:未被中断;并且清楚中断标记。

public class ThreadInterrupt extends Thread{

    public static void main(String[] args) throws InterruptedException {
        ThreadInterrupt threadInterrupt = new ThreadInterrupt();
        threadInterrupt.start();
        sleep(300);
        threadInterrupt.interrupt();
    }

    @Override
    public void run() {
        for (int i = 0; i < 60000; i++) {
            System.out.println("输出为:"+(i+1));
            // 判断当前线程是否中断
            if (ThreadInterrupt.currentThread().isInterrupted()) {
                System.out.println("线程已经中断。");
                // interrrupted 判断当前线程是否中断 并且清楚中断标记。
                if (Thread.interrupted()) {
                    System.out.println("二次判断线程已中断。");
                }
            }
            if (!Thread.currentThread().isInterrupted()) {
                System.out.println("第二次被唤起线程");
            }
        }
    }
}

相关文章

  • 线程中断和终止

    线程中断的定义:(我的理解)就是中断不同于终止,终止是将处于阻塞状态的线程终止,清理资源.通常中断的线程不在执行状...

  • 线程中断

    Java的中断是一种协作机制,线程中断不会终止线程的运行,但是可以通过线程中断来实现终止线程运行。 线程在不同状态...

  • 泥瓦匠聊并发编程基础篇:线程中断和终止

    1 线程中断 1.1 什么是线程中断? 线程中断是线程的标志位属性。而不是真正终止线程,和线程的状态无关。线程中断...

  • 阿里P8聊并发编程:线程中断和终止

    一、线程中断 1.什么是线程中断? 线程中断是线程的标志位属性。而不是真正终止线程,和线程的状态无关。线程中断过程...

  • 多线程(三)

    线程终止 1.run方法正常退出,线程自然终止2.因为一个没有捕获的异常终止了run方法,线程意外终止 线程中断 ...

  • 【多线程】——3.线程的中断

    线程中断的概念 java线程中断是一种协作机制 通过中断并不能直接终止线程的运行 需要被中断的线程自己处理中断 (...

  • Thread的interrupt中断操作

    线程中断意思 中断的意思,即给线程附上一个中断的标记,是标记,给个标记只是表明请求终止这个线程,并不是立马就给终止...

  • 线程的基本操作

    线程的基本操作 •线程状态切换 •终止线程(stop) •中断线程(interrupt) •挂起(suspend)...

  • 多线程基础

    多线程基础总结 一、线程中断 interrupt方法用来请求终止线程。 1. interrupt置位中断标志位 当...

  • interrupt与stop的区别

    stop是由系统强制终止执行,线程中断则是给目标线程发送一个中断信号,如果目标线程没有接收中断信号并结束线程,线程...

网友评论

      本文标题:线程终止,中断

      本文链接:https://www.haomeiwen.com/subject/ygdgtltx.html