如何让线程安全地停止工作

目录

目标

概念

代码演练

演示stop()方法:强行中断线程(已废弃)

演示interrupt()方法:提醒线程中断

演示isInterrupted()方法:检查线程是否中断

演示interrupted()方法:检查线程是否中断,并清除中断标志

实现接口的创建线程的方式如何停止线程

线程阻塞后如何停止线程


目标

  • 掌握正确中断线程的方法,能区分stop()、interrupt()、interrupted()、isInterrupted()之间的区别;
  • 掌握如何在阻塞的情况下中断线程。

概念

java.lang.Thread类中断方法
方法描述
stop()强制线程停止执行,这种方法本身就是不安全的,已弃用。
interrupt()提醒线程中断,并不中断线程。
interrupted()
  1. 测试当前线程是否已中断,已中断,返回true;否则返回false;
  2. 清除线程的中断状态,即线程中断后再调用该方法返回法false。
isInterrupted()当前线程已中断,返回true;否则返回false。

代码演练

演示stop()方法:强行中断线程(已废弃)

package com.ctx.concurrent.thread;

public class ThreadTest extends Thread {
    @Override
    public void run() {
        System.out.println("正在调用run()方法…………");
        try {
            Thread.sleep(2000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("run()方法结束。");
    }

    public static void main(String[] args) {
        ThreadTest threadTest = new ThreadTest();
        threadTest.start();
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        //没有输出这一句话:run()方法结束。
        //说明该方法是强制中断线程,该方法已经废弃了。
        //说明了java中的线程属于协作式线程,而不是抢占式线程。
        threadTest.stop();
    }
}

演示interrupt()方法:提醒线程中断

package com.ctx.concurrent.thread;

public class ThreadTest extends Thread {
    @Override
    public void run() {
        while(true){
            System.out.println("正在调用run()方法…………");
        }
    }

    public static void main(String[] args) {
        ThreadTest threadTest = new ThreadTest();
        threadTest.start();
        //线程一直没有退出,由此可见该方法并不能中断线程。
        threadTest.interrupt();
    }
}

演示isInterrupted()方法:检查线程是否中断

package com.ctx.concurrent.thread;

import lombok.SneakyThrows;

public class ThreadTest extends Thread {
    @SneakyThrows
    @Override
    public void run() {
        System.out.println("线程的状态:"+isInterrupted());
        //先后打印了:线程的状态:false、线程的状态:true
        //表示isInterrupted()起到了查看线程是否中断的作用。
        //当前线程已中断,则为true;否则为false。
        while(!isInterrupted()){
            System.out.println("正在调用run()方法…………");
        }
        System.out.println("线程的状态:"+isInterrupted());
    }

    public static void main(String[] args) throws InterruptedException {
        ThreadTest threadTest = new ThreadTest();
        threadTest.start();
        Thread.sleep(2);
        threadTest.interrupt();
    }
}

演示interrupted()方法:检查线程是否中断,并清除中断标志

package com.ctx.concurrent.thread;

import lombok.SneakyThrows;

public class ThreadTest extends Thread {
    @SneakyThrows
    @Override
    public void run() {
        System.out.println("线程的状态:"+isInterrupted());
        //功能一:测试当前线程是否已中断,已中断,返回true;否则返回false;
        //功能二:清除线程的中断状态,即线程中断后再调用该方法返回法false。
        //当前线程已中断,则为true;否则为false。
        while(!interrupted()){
            System.out.println("正在调用run()方法…………");
        }
        System.out.println("线程的状态:"+isInterrupted());
    }

    public static void main(String[] args) throws InterruptedException {
        ThreadTest threadTest = new ThreadTest();
        threadTest.start();
        Thread.sleep(2);
        threadTest.interrupt();
    }
}

实现接口的创建线程的方式如何停止线程

/*使用实现接口的方式实现多线程,如何调用中断操作?*/
package com.ctx.concurrent.thread;

public class RunTest implements Runnable{
    @Override
    public void run() {
        //通过获取当前线程来调用各种相关方法。
        //功能一:测试当前线程是否已中断,已中断,返回true;否则返回false;
        //功能二:清除线程的中断状态,即线程中断后再调用该方法返回法false。
        //当前线程已中断,则为true;否则为false。
        while(!Thread.currentThread().isInterrupted()){
            System.out.println("正在调用run()方法…………");
        }
        System.out.println("线程的状态:"+Thread.currentThread().isInterrupted());
    }
}

线程阻塞后如何停止线程

package com.ctx.concurrent.thread;

import lombok.SneakyThrows;

public class ThreadTest extends Thread {
    @Override
    public void run() {
        System.out.println("A、线程的状态:"+isInterrupted());
        //功能一:测试当前线程是否已中断,已中断,返回true;否则返回false;
        //功能二:清除线程的中断状态,即线程中断后再调用该方法返回法false。
        //当前线程已中断,则为true;否则为false。
        while(!isInterrupted()){
            try {
                //sleep方法抛出了中断异常,说明sleep方法能感知到中断操作。
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                System.out.println("B、线程的状态:"+isInterrupted());
                //中断线程,使isInterrupted()值为true。
                //如果注释掉该方法,则程序正常运行,这也说明了isInterrupted()具有:功能二:清除线程的中断状态,即线程中断后再调用该方法返回法false。
                interrupt();
                System.out.println("C、线程的状态:"+isInterrupted());
                e.printStackTrace();
            }
            System.out.println("正在调用run()方法…………");
        }
        System.out.println("线程的状态:"+isInterrupted());
    }

    public static void main(String[] args) throws InterruptedException {
        ThreadTest threadTest = new ThreadTest();
        threadTest.start();
        Thread.sleep(2);
        threadTest.interrupt();
    }
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值