ThreadPoolExecutor and the queue

10,794

Solution 1

Worker threads are spawned as tasks arrive by execute, and these are the ones that interact with the underlying work queue. You need to prestart the workers if you begin with a non-empty work queue. See the implementation in OpenJDK 7.

I repeat, the workers are the ones that interact with the work queue. They are only spawned on demand when passed via execute. (or the layers above it, e.g. invokeAll, submit, etc.) If they are not started, it will not matter how much work you add to the queue, since there is nothing checking it as there are no workers started.

ThreadPoolExecutor does not spawn worker threads until necessary or if you pre-empt their creation by the methods prestartAllCoreThreads and prestartCoreThread. If there are no workers started, then there is no way any of the work in your queue is going to be done.

The reason adding an initial execute works is that it forces the creation of a sole core worker thread, which then can begin processing the work from your queue. You could also call prestartCoreThread and receive similar behavior. If you want to start all the workers, you must call prestartAllCoreThreads or submit that number of tasks via execute.

See the code for execute below.

/**
 * 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
 */
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);
}

Solution 2

A BlockingQueue is not a magic thread dispatcher. If you submit Runnable objects to the queue and there are no running threads to consume those tasks, they of course will not be executed. The execute method on the other hand will automatically dispatch threads according to the thread pool configuration if it needs to. If you pre-start all of the core threads, there will be threads there to consume tasks from the queue.

Share:
10,794
Cratylus
Author by

Cratylus

Updated on July 23, 2022

Comments

  • Cratylus
    Cratylus almost 2 years

    I thought that using ThreadPoolExecutor we can submit Runnables to be executed either in the BlockingQueue passed in the constructor or using the execute method.
    Also my understanding was that if a task is available it will be executed.
    What I don't understand is the following:

    public class MyThreadPoolExecutor {  
    
        private static ThreadPoolExecutor executor;  
    
        public MyThreadPoolExecutor(int min, int max, int idleTime, BlockingQueue<Runnable> queue){  
            executor = new ThreadPoolExecutor(min, max, 10, TimeUnit.MINUTES, queue);   
            //executor.prestartAllCoreThreads();  
        }  
    
        public static void main(String[] main){
            BlockingQueue<Runnable> q = new LinkedBlockingQueue<Runnable>();
            final String[] names = {"A","B","C","D","E","F"};  
            for(int i = 0; i < names.length; i++){  
                final int j = i;  
                q.add(new Runnable() {  
    
                    @Override  
                    public void run() {  
                        System.out.println("Hi "+ names[j]);  
    
                    }  
                });         
            }  
            new MyThreadPoolExecutor(10, 20, 1, q);   
            try {  
                TimeUnit.SECONDS.sleep(5);  
            } catch (InterruptedException e) {  
                // TODO Auto-generated catch block  
                e.printStackTrace();  
            }  
            /*executor.execute(new Runnable() {  
    
                @Override  
                public void run() {  
    
                    System.out.println("++++++++++++++");  
    
                }   
            });  */
            for(int i = 0; i < 100; i++){  
                final int j = i;  
                q.add(new Runnable() {   
    
                    @Override  
                    public void run() {  
                        System.out.println("Hi "+ j);  
    
                    }  
                });  
            }   
    
    
        }  
    
    
    }
    

    This code does not do absolutely anything unless I either uncomment the executor.prestartAllCoreThreads(); in the constructor OR I call execute of the runnable that prints System.out.println("++++++++++++++"); (it is also commented out).

    Why?
    Quote (my emphasis):

    By default, even core threads are initially created and started only when new tasks arrive, but this can be overridden dynamically using method prestartCoreThread() or prestartAllCoreThreads(). You probably want to prestart threads if you construct the pool with a non-empty queue.

    Ok. So my queue is not empty. But I create the executor, I do sleep and then I add new Runnables to the queue (in the loop to 100).
    Doesn't this loop count as new tasks arrive?
    Why doesn't it work and I have to either prestart or explicitely call execute?