Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/multithreading/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java ExecutorService.submit如何提交工作_Java_Multithreading - Fatal编程技术网

Java ExecutorService.submit如何提交工作

Java ExecutorService.submit如何提交工作,java,multithreading,Java,Multithreading,我试图了解Executor.submit(Runnable)是如何工作的。假设我们使用ExecutorService创建一个大小为2的线程池 我们有一个名为Runner implements Runnable的类 ExecutorService.newFixedThreadPool(2); for (int i = 0; i <5; i++){ ExecutorService.submit(new Runner()); } ExecutorService.newFixedThread

我试图了解Executor.submit(Runnable)是如何工作的。假设我们使用ExecutorService创建一个大小为2的线程池

我们有一个名为Runner implements Runnable的类

ExecutorService.newFixedThreadPool(2);
for (int i = 0; i <5; i++){
  ExecutorService.submit(new Runner());
}
ExecutorService.newFixedThreadPool(2);

对于(int i=0;i,服务使用一个队列来存储传入的可运行对象。一旦有线程可用于执行工作,服务就会使用该线程来执行下一个到期的可运行对象

可运行程序是而不是线程。您的示例将创建组成池的两个线程;然后随着时间的推移,5个运行程序对象将被分派到这两个线程中的一个


这就是它的全部内容。

不,您正在创建包含2个线程的线程池,Executor服务将等到池中有可用线程时,才将下一个任务提交给可用线程。

您可以检查from grepcode的实现

AbstractExecutorService
是的基类,它实现

检查
工人
在同一类中的实施情况

/**
 * Class Worker mainly maintains interrupt control state for
 * threads running tasks, along with other minor bookkeeping.
 * This class opportunistically extends AbstractQueuedSynchronizer
 * to simplify acquiring and releasing a lock surrounding each
 * task execution.  This protects against interrupts that are
 * intended to wake up a worker thread waiting for a task from
 * instead interrupting a task being run.  We implement a simple
 * non-reentrant mutual exclusion lock rather than use ReentrantLock
 * because we do not want worker tasks to be able to reacquire the
 * lock when they invoke pool control methods like setCorePoolSize.
 */

我明白了!谢谢你的回复。我该怎么做,我没有看到任何扣子。我想它就在那里,只是看得不够锐利:)谢谢你的回复。
 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);

/**
 * Checks if a new worker can be added with respect to current
 * pool state and the given bound (either core or maximum). If so,
 * the worker count is adjusted accordingly, and, if possible, a
 * new worker is created and started running firstTask as its
 * first task. This method returns false if the pool is stopped or
 * eligible to shut down. It also returns false if the thread
 * factory fails to create a thread when asked, which requires a
 * backout of workerCount, and a recheck for termination, in case
 * the existence of this worker was holding up termination.
 *
 * @param firstTask the task the new thread should run first (or
 * null if none). Workers are created with an initial first task
 * (in method execute()) to bypass queuing when there are fewer
 * than corePoolSize threads (in which case we always start one),
 * or when the queue is full (in which case we must bypass queue).
 * Initially idle threads are usually created via
 * prestartCoreThread or to replace other dying workers.
 *
 * @param core if true use corePoolSize as bound, else
 * maximumPoolSize. (A boolean indicator is used here rather than a
 * value to ensure reads of fresh values after checking other pool
 * state).
 * @return true if successful
 */

 private boolean addWorker(Runnable firstTask, boolean core)
/**
 * Class Worker mainly maintains interrupt control state for
 * threads running tasks, along with other minor bookkeeping.
 * This class opportunistically extends AbstractQueuedSynchronizer
 * to simplify acquiring and releasing a lock surrounding each
 * task execution.  This protects against interrupts that are
 * intended to wake up a worker thread waiting for a task from
 * instead interrupting a task being run.  We implement a simple
 * non-reentrant mutual exclusion lock rather than use ReentrantLock
 * because we do not want worker tasks to be able to reacquire the
 * lock when they invoke pool control methods like setCorePoolSize.
 */