Reputation: 450
What is the order of execution for submitted task to the ExecutorService object ?
Scenario: Let's assume for a moment Executor Thread-pool size is 5 and i have submitted 20 runnable task to it and we know at a time only five task can be executed and remaining will be waiting in bucket. So my question is in what order submitted task get executed. Does it follow FIFO data structure or it randomly pick the task from bucket.
Also, is there any way to specify in which order it should get executed.
example:
ExecutorService executor = Executors.newFixedThreadPool(5);
for(int i =0; i < 100; i++){
executor.submit(() -> {
System.out.println("print task");
})
}
Upvotes: 1
Views: 1724
Reputation: 5103
It uses LinkedBlockingQueue to store the pending tasks. So it follows FIFO order for the pending tasks. Please find the java doc along with the method signature.
/**
* Creates a thread pool that reuses a fixed number of threads
* operating off a shared unbounded queue. At any point, at most
* {@code nThreads} threads will be active processing tasks.
* If additional tasks are submitted when all threads are active,
* they will wait in the queue until a thread is available.
* If any thread terminates due to a failure during execution
* prior to shutdown, a new one will take its place if needed to
* execute subsequent tasks. The threads in the pool will exist
* until it is explicitly {@link ExecutorService#shutdown shutdown}.
*
* @param nThreads the number of threads in the pool
* @return the newly created thread pool
* @throws IllegalArgumentException if {@code nThreads <= 0}
*/
public static ExecutorService newFixedThreadPool(int nThreads) {
return new ThreadPoolExecutor(nThreads, nThreads,
0L, TimeUnit.MILLISECONDS,
new LinkedBlockingQueue<Runnable>());
}
Upvotes: 1
Reputation: 14732
It depends on the thread pool's and the queue's implementations.
In the case of ThreadPoolExecutor, the queue is almost always a SynchronousQueue, a LinkedBlockingQueue or an ArrayBlockingQueue. ArrayBlockingQueue and LinkedBlockingQueue guaranty FIFO ordering. For SynchronousQueue, it depends on FIFO capability of synchronization mechanisms, since a SynchronousQueue can only keep one element at a time. You'd better assume it's not guaranteed.
In the case of ScheduledThreadPoolExecutor, the queue is a DelayQueue. Basicly it's a priority queue, most probably implemented using an heap datastructure. Tasks will be taken from the queue in the order of their delay, but FIFO isn't guaranteed for two tasks having the same delay.
However, you must consider that, after all, picking a task from the queue doesn't mean that it will be executed immediately, given the preamptive nature of threads. The main loop of an executor looks basicly like this:
public void run () {
while(!terminated) {
Runnable task = queue.take();
task.run();
}
}
Consider that two threads are running this code. This scenario may happen:
Runnable task = queue.take()
Runnable task = queue.take()
task.run();
task.run();
And voilà, the second task come after in the queue, but is executed before the first task. In practice, this happens all the time.
At the end, you can't, and shouldn't, assume anything.
Upvotes: 1