1.1 线程池-初始化

1.1 线程池-初始化

初始化

每一个属性都很重要

public ThreadPoolExecutor(int corePoolSize,
                              int maximumPoolSize,
                              long keepAliveTime,
                              TimeUnit unit,
                              BlockingQueue<Runnable> workQueue,
                              ThreadFactory threadFactory,
                              RejectedExecutionHandler handler) {
        if (corePoolSize < 0 ||
            maximumPoolSize <= 0 ||
            maximumPoolSize < corePoolSize ||
            keepAliveTime < 0)
            throw new IllegalArgumentException();
        if (workQueue == null || threadFactory == null || handler == null)
            throw new NullPointerException();
        this.acc = System.getSecurityManager() == null ?
                null :
                AccessController.getContext();
	  //核心线程数
        this.corePoolSize = corePoolSize;
		//最大线程池数量
        this.maximumPoolSize = maximumPoolSize;
		//任务队列
        this.workQueue = workQueue;
		//线程池中空闲线程等待工作的超时时间
		//线程池中线程数量大于corePoolSize(核心线程数量)或设置了allowCoreThreadTimeOut(是否允许空闲核心线程超时)时,线程会根据keepAliveTime的值进行活性检查,一旦超时便销毁线程
		//会转成纳秒
        this.keepAliveTime = unit.toNanos(keepAliveTime);
		//线程工厂类 定义了如何新建一个线程
        this.threadFactory = threadFactory;
		//拒绝策略
        this.handler = handler;
    }

还有几个默认的属性非常重要,是一堆关于状态控制的变量,基本搞懂了它,线程池的运转就搞懂了。

状态


   //Integer 共有32位,最右边29位表示工作线程数,最左边3位表示线程池状态。
  //注:简单的说,3个二进制位可以表示从0-7的8个不同的数值(第1处)
    private static final int COUNT_BITS = Integer.SIZE - 3;
	//000-11111111111111111111111111111,类似于子网掩码,用于位的与运算
   //得到最左边的3位,还是右边的29位
   //CAPACITY是最大容量536870911,因为1左移29位之后-1,导致最高三位为0,而其余29位全部为1
    private static final int CAPACITY   = (1 << COUNT_BITS) - 1;

//ctl用于表示线程池状态和有效线程数量,最高三位表示线程池的状态,其余低位表示有效线程数量
//初始化之后ctl等于RUNNING的值,即默认状态是运行状态,线程数量为0
  private final AtomicInteger ctl = new AtomicInteger(ctlOf(RUNNING, 0));
  
    // runState is stored in the high-order bits
	//1110 0000 0000 0000 0000 0000 0000 0000最高三位为111
	十进制值:-563,870,912
	 //此状态便是线程池能够接受的新任务
    private static final int RUNNING    = -1 << COUNT_BITS;
	//000-00000000000000000000000000000
	//十进制值:0
	//此状态不再接受新任务,但可以继续执行队列中的任务
    private static final int SHUTDOWN   =  0 << COUNT_BITS;
	
	//001-00000000000000000000000000000,十进制值:563,870,912
	//此状态全面拒绝,并终止正在处理的任务
    private static final int STOP       =  1 << COUNT_BITS;
	/010-00000000000000000000000000000,十进制值:1073,741,824
	//此状态表示所有的任务已经被终止
    private static final int TIDYING    =  2 << COUNT_BITS;
	//011-00000000000000000000000000000,十进制值:1610,612,736
	//此状态表示已清理完现场 
    private static final int TERMINATED =  3 << COUNT_BITS;

    // Packing and unpacking ctl
	//与运算,比如 001-00000000000000000000000100011,表示67个工作线程
	//~CAPACITY是 111-00000000000000000000000000000
	// c&~CAPACITY 即得到最左边3位 001
	// 表示线程处于stop 状态
    private static int runStateOf(int c)     { return c & ~CAPACITY; }
	//CAPACITY是 000-11111111111111111111111111111
	// c&~CAPACITY 即得到右边29位 就是线程数量
    private static int workerCountOf(int c)  { return c & CAPACITY; }
	//把左边3位与右边29位按或运算,合并成一个值
    private static int ctlOf(int rs, int wc) { return rs | wc; }

 
    /*
     * Bit field accessors that don"t require unpacking ctl.
     * These depend on the bit layout and on workerCount being never negative.
     */

    private static boolean runStateLessThan(int c, int s) {
        return c < s;
    }

    private static boolean runStateAtLeast(int c, int s) {
        return c >= s;
    }
   //五种状态的值大小关系:RUNNING<SHUTDOWN<STOP<TIDYING<TERMINATED
   // 所以小于shutdown就剩下RUNNING了
    private static boolean isRunning(int c) {
        return c < SHUTDOWN;
    }

workers

 private final ReentrantLock mainLock = new ReentrantLock();
 //任务队列 一个任务代表一个线程
private final HashSet<Worker> workers = new HashSet<Worker>();

ThreadFactory

public interface ThreadFactory {

    /**
     * Constructs a new {@code Thread}.  Implementations may also initialize
     * priority, name, daemon status, {@code ThreadGroup}, etc.
     *
     * @param r a runnable to be executed by new thread instance
     * @return constructed thread, or {@code null} if the request to
     *         create a thread is rejected
     */
    Thread newThread(Runnable r);
}
hmoban主题是根据ripro二开的主题,极致后台体验,无插件,集成会员系统
自学咖网 » 1.1 线程池-初始化