线程池如何保证核心线程一直存活

转载请注明出处:

  查看  ThreadPoolExecutor 类中的 getTask 方法,这个方法可以保持核心线程在没有任务的时候也可以一直处于存活状态

/**
     * Performs blocking or timed wait for a task, depending on
     * current configuration settings, or returns null if this worker
     * must exit because of any of:
     * 1. There are more than maximumPoolSize workers (due to
     *    a call to setMaximumPoolSize).
     * 2. The pool is stopped.
     * 3. The pool is shutdown and the queue is empty.
     * 4. This worker timed out waiting for a task, and timed-out
     *    workers are subject to termination (that is,
     *    {@code allowCoreThreadTimeOut || workerCount > corePoolSize})
     *    both before and after the timed wait, and if the queue is
     *    non-empty, this worker is not the last thread in the pool.
     *
     * @return task, or null if the worker must exit, in which case
     *         workerCount is decremented
     */
    private Runnable getTask() {
        boolean timedOut = false; // Did the last poll() time out?

        for (;;) {
            int c = ctl.get();
            int rs = runStateOf(c);

            // Check if queue empty only if necessary.
            if (rs >= SHUTDOWN && (rs >= STOP || workQueue.isEmpty())) {
                decrementWorkerCount();
                return null;
            }

            int wc = workerCountOf(c);

            // Are workers subject to culling?
            boolean timed = allowCoreThreadTimeOut || wc > corePoolSize;

            if ((wc > maximumPoolSize || (timed && timedOut))
                && (wc > 1 || workQueue.isEmpty())) {
                if (compareAndDecrementWorkerCount(c))
                    return null;
                continue;
            }

            try {
                Runnable r = timed ?
                    workQueue.poll(keepAliveTime, TimeUnit.NANOSECONDS) :
                    workQueue.take();
                if (r != null)
                    return r;
                timedOut = true;
            } catch (InterruptedException retry) {
                timedOut = false;
            }
        }
    }

  核心在于 workQueue.poll 和 workQueue.take() 两个方法,且前面会判断 当前线程数,如果 当前线程数等于核心线程数时,就会调用 take 方法,让线程向队列获取任务的状态一直保持阻塞状态,通过这个方法就可以保证核心线程一直存活状态。

  看下 take 与 poll 方法的区别:

  take:会响应中断,会一直阻塞直到取得元素或当前线程中断。如果队列中没有数据,则线程wait释放CPU
  poll:会响应中断,会阻塞,阻塞时间参照方法里参数 timeout,timeUnit,当阻塞时间到了还没取得元素会返回null

  

 

posted @ 2022-06-09 00:47  香吧香  阅读(1948)  评论(0编辑  收藏  举报