Java源码解析线程池ThreadPoolExecutor的execute方法

版权声明:博主保留一切权利,转载请注明出处。 https://blog.csdn.net/li_canhui/article/details/85700359

线程池有很多方法,其中最常用的一个是execute()方法,它允许用户把任务提交给线程池进行执行。

首先看一下execute()的功能介绍,如下图。在将来的某个时间执行给定的任务。给定的任务有可能在一个新线程中执行,也有可能在已经存在的线程中执行。如果任务无法被提交,不管是因为线程池已经关闭了,还是因为线程池已经达到最大容量,那么任务会被拒绝策略来处理。

    /**
     * Executes the given task sometime in the future.  The task
     * may execute in a new thread or in an existing pooled thread.
     *
     * If the task cannot be submitted for execution, either because this
     * executor has been shutdown or because its capacity has been reached,
     * the task is handled by the current {@code RejectedExecutionHandler}.
     *
     * @param command the task to execute
     * @throws RejectedExecutionException at discretion of
     *         {@code RejectedExecutionHandler}, if the task
     *         cannot be accepted for execution
     * @throws NullPointerException if {@code command} is null
     */

下面是execute的代码。execute处理任务的逻辑如下。

1,如果少于核心线程数的线程在运行,那么尝试用给定任务启动一个新的线程。调用addWorker会自动检查线程池的运行状态和运行的线程数量。

2,如果一个任务可以被成功的加入到队列中,然后我们仍然需要再检查一次,我们是否应当增加一个线程(因为从上次检查完之后已经存在线程们死了),还是自从我们进入这个方法后线程池关闭了。所以我们重新检查状态,如果线程池已经关闭了,那么就回退入队操作,或者如果没有线程时启动一个新的线程。

3,如果我们不能把任务加到队列中,那么我们尝试增加一个新的线程。如果失败了,那么我们就知道线程池已经关闭了,或者已经饱和了,所以我们会决绝这个任务。

    public void execute(Runnable command) {
        if (command == null)
            throw new NullPointerException();
        /*
         * Proceed in 3 steps:
         *
         * 1. If fewer than corePoolSize threads are running, try to
         * start a new thread with the given command as its first
         * task.  The call to addWorker atomically checks runState and
         * workerCount, and so prevents false alarms that would add
         * threads when it shouldn't, by returning false.
         *
         * 2. If a task can be successfully queued, then we still need
         * to double-check whether we should have added a thread
         * (because existing ones died since last checking) or that
         * the pool shut down since entry into this method. So we
         * recheck state and if necessary roll back the enqueuing if
         * stopped, or start a new thread if there are none.
         *
         * 3. If we cannot queue task, then we try to add a new
         * thread.  If it fails, we know we are shut down or saturated
         * and so reject the task.
         */
        int c = ctl.get();
        if (workerCountOf(c) < corePoolSize) {
            if (addWorker(command, true))
                return;
            c = ctl.get();
        }
        if (isRunning(c) && workQueue.offer(command)) {
            int recheck = ctl.get();
            if (! isRunning(recheck) && remove(command))
                reject(command);
            else if (workerCountOf(recheck) == 0)
                addWorker(null, false);
        }
        else if (!addWorker(command, false))
            reject(command);
    }

猜你喜欢

转载自blog.csdn.net/li_canhui/article/details/85700359