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.
source share