zoukankan      html  css  js  c++  java
  • Thread.interrupted概述

    package org.apache.ibatis.cache;
    
    import org.junit.Test;
    
    /**
     * 
     * 2021/7/14 7:37 下午
     */
    public class InteruprTest {
        /**
         *
         * Thread.interrupted(); 标志位恢复
         * Thread.currentThread().isInterrupted() 判断当前线程是否中断 可重复调用
         * @throws InterruptedException
         */
        @Test
        public void m1(){
            Thread.currentThread().interrupt();
            System.out.println("mainThread:"+Thread.currentThread().isInterrupted()); //true
            Thread t = new Thread(()->{
                while (true) {
                    System.out.println("t线程中断标识"+Thread.currentThread().isInterrupted());
                    if (Thread.currentThread().isInterrupted()) {
                        System.out.println("t线程中断标识"+Thread.currentThread().isInterrupted()); // true
                        System.out.println("t线程中断标识 interrupted = "+Thread.interrupted()); // true  重置为false
                        System.out.println("t线程中断标识"+Thread.currentThread().isInterrupted()); // false
                        break;
                    }
                }
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    System.out.println("sleep catch会重置标识 t线程中断标识"+Thread.currentThread().isInterrupted());  //false
                    System.out.println("t 线程触发中断");
                }
                System.out.println("hello");
            });
            t.start();
    
            try {
                Thread.sleep(1);
            } catch (InterruptedException e) {
                System.out.println("main 线程触发中断"); // 触发
                e.printStackTrace();
            }
            t.interrupt();
            System.out.println("111="+t.isInterrupted()); //true
        }
    }
    
    
  • 相关阅读:
    04邻接表深度和广度遍历DFS_BFS
    03邻接矩阵的深度和广度遍历的C语言实现
    02邻接表创建的C语言实现
    01邻接矩阵的创建C语言实现
    GUN的相关使用
    JAVA学习笔记
    排序的C语言实现
    线索二叉树的C语言实现
    maven配置logback
    多线程概念
  • 原文地址:https://www.cnblogs.com/albertXe/p/15014381.html
Copyright © 2011-2022 走看看