美文网首页
线程池状态(gold_axe)

线程池状态(gold_axe)

作者: 胖达_4b7e | 来源:发表于2020-10-24 23:12 被阅读0次

ThreadPoolExecutor 里面 有这些状态

     * The runState provides the main lifecycle control, taking on values:
     *
     *   RUNNING:  Accept new tasks and process queued tasks
     *   SHUTDOWN: Don't accept new tasks, but process queued tasks
     *   STOP:     Don't accept new tasks, don't process queued tasks,
     *             and interrupt in-progress tasks
     *   TIDYING:  All tasks have terminated, workerCount is zero,
     *             the thread transitioning to state TIDYING
     *             will run the terminated() hook method
     *   TERMINATED: terminated() has completed

状态设计机制

     * The main pool control state, ctl, is an atomic integer packing
     * two conceptual fields
     *   workerCount, indicating the effective number of threads
     *   runState,    indicating whether running, shutting down etc
    private final AtomicInteger ctl = new AtomicInteger(ctlOf(RUNNING, 0));

一个整形 存2值: 状态 线程数

前3bit是状态
111: RUNNING
000: SHUTDOWN
001: STOP
010: TIDYING
011: TERMINATED

    private static final int RUNNING    = -1 << COUNT_BITS;
    private static final int SHUTDOWN   =  0 << COUNT_BITS;
    private static final int STOP       =  1 << COUNT_BITS;
    private static final int TIDYING    =  2 << COUNT_BITS;
    private static final int TERMINATED =  3 << COUNT_BITS;
public class ThreadPoolStateTest {
    //ctl是线程池中一个非常重要的变量,以它的低29位表示线程池中处于RUNNING状态的线程个数,高3位表示线程池所处的状态
   public static int COUNT_BITS = Integer.SIZE - 3;
   public static int CAPACITY   = (1 << COUNT_BITS) - 1;

    public static void main(String[] args) {
        // <<表示 左移
        int RUNNING    = -1 << COUNT_BITS;
        int SHUTDOWN   =  0 << COUNT_BITS;
        int STOP       =  1 << COUNT_BITS;
        int TIDYING    =  2 << COUNT_BITS;
        int TERMINATED =  3 << COUNT_BITS;
        System.out.println("Integer.SIZE is :"+Integer.SIZE+"  , COUNT_BITS is : "+COUNT_BITS);
        System.out.println("CAPACITY is : "+CAPACITY+" , "+Integer.toBinaryString(CAPACITY));
      //五种状态中SHUTDOWN值等于0,RUNNING值小于0,其他三种状态STOP、TIDYING、TERMINATED值均大于0
        System.out.println("RUNNING    = " + RUNNING + " = " + Integer.toBinaryString(RUNNING));
        System.out.println("SHUTDOWN   = " + SHUTDOWN + " = " + Integer.toBinaryString(SHUTDOWN));
        System.out.println("STOP       = " + STOP + "  = 00" + Integer.toBinaryString(STOP));
        System.out.println("TIDYING    = " + TIDYING + " = 0" + Integer.toBinaryString(TIDYING));
        System.out.println("TERMINATED = " + TERMINATED + " = 0" + Integer.toBinaryString(TERMINATED));
    }
    //CAPACITY低29位全1高3位全0,它与c做与运算得到的就是当前运行的线程个数
    static int workerCountOf(int c)  {
        return c & CAPACITY;
    }

}

Integer.SIZE is :32  , COUNT_BITS is : 29
CAPACITY is : 536870911 , 11111111111111111111111111111
RUNNING    = -536870912 = 11100000000000000000000000000000
SHUTDOWN   = 0 = 0
STOP       = 536870912  = 00100000000000000000000000000000
TIDYING    = 1073741824 = 01000000000000000000000000000000
TERMINATED = 1610612736 = 01100000000000000000000000000000

Process finished with exit code 0

之间的切换

     * RUNNING -> SHUTDOWN
     *    On invocation of shutdown(), perhaps implicitly in finalize()
     * (RUNNING or SHUTDOWN) -> STOP
     *    On invocation of shutdownNow()
     * SHUTDOWN -> TIDYING
     *    When both queue and pool are empty
     * STOP -> TIDYING
     *    When pool is empty
     * TIDYING -> TERMINATED
     *    When the terminated() hook method has completed

3个事件, 把生命分成4段,SHUTDOWN和STOP 取其一

  • 1.关闭线程池
  • 2.任务都结束
  • 3.terminated()方法执行完

相关文章

  • 线程池状态(gold_axe)

    ThreadPoolExecutor 里面 有这些状态 状态设计机制 一个整形 存2值: 状态 线程数 前3bit...

  • Java线程池

    线程池 new Thread 弊端 线程池的好处? TreadPoolExecutor 线程池的几种状态 初始...

  • 多线程juc线程池

    java_basic juc线程池 创建线程池 handler是线程池拒绝策略 排队策略 线程池状态 RUNNIN...

  • shutdown、shutdownNow方法的理解

    shutdown() 1、当线程池调用该方法时,线程池的状态则立刻变成SHUTDOWN状态。此时,则不能再往线程池...

  • JUC线程池(4):线程池状态

    我们都知道,线程有5种状态:新建状态,就绪状态,运行状态,阻塞状态,死亡状态。线程池也有5种状态;然而,线程池不同...

  • ThreadPoolExecutor学习笔记

    线程池状态: 高3位表示"线程池状态"低29位表示"线程池中的任务数量" Worker

  • 线程池初探

    线程池架构图 线程池状态流转图 线程池主要参数介绍 corePoolSize: 核心线程数量 maximumPoo...

  • java线程状态和线程池

    本节总结线程相关知识:线程状态和线程池。1.线程的五个状态 关于如何终止线程,以下仅供参考: 2.线程池

  • 八、线程池剖析

    一、前置问题 线程的状态转换 为什么要使用线程池 线程池的继承体系 线程池使用的场景 线程数的设置规则 线程池的状...

  • 多线程 | 4.线程池

    Java并发编程:线程池的使用 线程池基础 请求队列 线程池维护一定数量的线程,当线程池在运行状态的线程数量达上...

网友评论

      本文标题:线程池状态(gold_axe)

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