Is ScheduledThreadPoolExecutor available to perform multiple tasks at once?

The documents for ScheduledThreadPoolExecutor say that - Tasks scheduled for the same runtime are included in the order received for the first time for the first time (FIFO).

Does this mean that tasks that MUST be performed at the same time are never performed at the same time. Instead, are they executed in FIFO order?

If this is true, then which class am I using, which is better than Timer , and also does not have this problem with FIFO?

+6
source share
3 answers

How ScheduledThreadPoolExecutor works, there is one β€œschedule” or main thread that checks the execution of tasks.

If he finds a job, he delegates it to the worker thread from the pool.

If several tasks are ready for execution, they are "pulled out" one by one, although after "shutting down" the subsequent processing is parallel for each Java definition.

If you have two tasks that are scheduled through the executor at the same time, the order in which they are completed can vary from start to start, and if you do not enter certain controls, such as locks, waiting, etc., to handle this , this is before scheduling a java thread (how java allocates time for threads on the kernel) to determine how and when it is processed. Please note that setting such locks, expectations, etc. - a deceptively difficult task, prone to race conditions, leading to unexpected dead ends, live castles, etc.

+4
source

It depends on the size of the thread pool. If you plan 1000 tasks for firing at midnight, and you only have 25 threads, then initially only 25 can be executed, and the rest should wait for the available threads. FIFO here refers to the order in which the worker transfers jobs to threads.

+1
source

Please note that the docs talk about the "inclusion" of tasks and that we are talking about the thread executor. :-)

This means that the tasks will wait until the appointed time, then they are processed as if they were placed in a regular ThreadPoolExecutor. If there are enough threads in the pool, all these tasks will be performed in parallel.

Only if you have more tasks than active threads in the pool, some tasks will have to wait.

+1
source

All Articles