• 正确停止线程(四)


    1.使用interrupt关闭线程

    /**
    * 使用interrupt关闭线程,注意不可强制,而是要判断true如果当前线程已被中断; false除此以外。!Thread.currentThread().isInterrupted()
    */
    public class RightWayStopThreadWithoutSleep implements Runnable{
    @Override
    public void run() {
    int num = 0;
    while (!Thread.currentThread().isInterrupted() && num<=Integer.MAX_VALUE /2){
    if(num%10000==0){
    System.out.println(num+"是1000的倍数");
    }
    num++;
    }
    System.out.println("结束啦");
    }
    public static void main(String[] args) throws InterruptedException {
    Thread thread = new Thread(new RightWayStopThreadWithoutSleep());
    thread.start();
    Thread.sleep(1000);
    thread.interrupt();
    }
    }

    2.堵塞状态中断的方式是抛出异常:

     1.循环里面使用了Thread.sleep(1000);代表每次运行都延迟1s,最下面的5s是整个线程运行5s然后会抛出异常结束线程
    2.循环里面可以不用Thread.currentThread().isInterrupted()来判断线程是否要到点关闭了

    /**
    * 如果在执行过程中,每次循环都会调用sleep或wait等方法,那么不需要每次迭代都检查是否已中断
    */
    public class RightWayStopThreadWithSleepEveryLoop {
    public static void main(String[] args) throws InterruptedException {
    Runnable runnable = ()->{
    int num = 0;
    try {
    while (num<=10000000){
    if (num%10 ==0){
    System.out.println(num+"是10的倍数");
    }
    num+=10;
    Thread.sleep(1000);
    System.out.println("hhh");
    }
           } catch (InterruptedException e) {
    e.printStackTrace();
    }
    };
    Thread thread = new Thread(runnable);
    thread.start();
    Thread.sleep(5000);
    thread.interrupt();

    }
    }

     

    3.把try-catch放在while里面堵塞状态中断会失效:

    4.把try-catch放在while里面堵塞状态中断会失效怎么办?:1.传递中断

    1.这里try-catch还是在while里面,最好是在catch里面做出选择停止线程,这个demo是把休息时间放在了throwInMethod这个方法里面,也try-catch了,但是呢

    /**
    * 最佳时间 catch了InterruptedEcxetion之后的优先选择
    * :在方法签名中抛出异常
    * 那么在run()就会强制try/catch
    */
    public class RightWayStopThreadInProd implements Runnable{
    @Override
    public void run() {
    while (true && !Thread.currentThread().isInterrupted()){
    System.out.println("我来啦");
    new RightWayStopThreadInProd().throwInMethod();
    }
    }
    private void throwInMethod(){
    try {
    Thread.sleep(2000);
    } catch (InterruptedException e) {
    e.printStackTrace();
    }
    }
    public static void main(String[] args) throws InterruptedException {
    Thread thread = new Thread(new RightWayStopThreadInProd());
    thread.start();
    Thread.sleep(5000);
    thread.interrupt();
    }
    }

    2.这里try-catch还是在while里面,最好是在catch里面做出选择停止线程,这个demo是把休息时间放在了throwInMethod这个方法里面,也try-catch了,但是呢try-catch是在使用的地方try-catch的这样可以根据不同的需求做不同的处理
    /**
    * 最佳时间 catch了InterruptedEcxetion之后的优先选择
    * :在方法签名中抛出异常
    * 那么在run()就会强制try/catch
    */
    @Slf4j
    public class RightWayStopThreadInProd implements Runnable{
    @Override
    public void run() {
    while (true && !Thread.currentThread().isInterrupted()){
    System.out.println("我来啦");
    try {
    new RightWayStopThreadInProd().throwInMethod();
    } catch (InterruptedException e) {
    log.error("我在这中断了,我要看我跳出来了之后干嘛",e.getMessage());
    break;
    }
    }
    }
    private void throwInMethod() throws InterruptedException {
    Thread.sleep(2000);
    }
    public static void main(String[] args) throws InterruptedException {
    Thread thread = new Thread(new RightWayStopThreadInProd());
    thread.start();
    Thread.sleep(5000);
    thread.interrupt();
    }
    }

    5.把try-catch放在while里面堵塞状态中断会失效怎么办?:2.恢复中断

    既然是因为每次catch之后会把interrupt给清掉,导致无法获取是否中断,那么在catch中:Thread.currentThread().interrupt();实现中断就可以了

  • 相关阅读:
    Linux 的文件软链接如何删除
    mysql-xtrabackup备份sh: xtrabackup_56: command not found与error while loading shared libraries: libssl.so.6: cannot open shared object file: No such file or directory
    How To Upgrade ASMLib Kernel Driver as Part of Kernel Upgrade? (文档 ID 1391807.1)
    [trouble] error connecting to master 'repl@192.168.1.107:3306'
    Troubleshooting 10g and 11.1 Clusterware Reboots (文档 ID 265769.1)
    "Last_IO_Error: Fatal error: The slave I/O thread stops because master and slave have equal MySQL server UUIDs
    RMAN-06900 RMAN-06901 ORA-19921
    一则ORACLE进程都在但是无法进入实例的问题
    VirtualBox下Win7下CPU高占用的一次故障解决
    netcore之mysql中文乱码问题解决记录
  • 原文地址:https://www.cnblogs.com/chwl-ljx/p/15500219.html
Copyright © 2020-2023  润新知