I thought that using ThreadPoolExecutor we can submit Runnable
s 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 Runnable
s 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
?
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 callprestartCoreThread
and receive similar behavior. If you want to start all the workers, you must callprestartAllCoreThreads
or submit that number of tasks viaexecute
.See the code for
execute
below.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.