We wait in a mount of time for all tasks to finish.
generate CompletableFuture from each task
collect them all
wrap up and wait in specific timeout
From a macroscopic perspective, play method shall run within 1000ms, while from a microscopic perspective, any task may have not begin to run or have not finished.
What’s the Matter
Every task should begin to run as quickly as possible, and finish up in a short time. If any task is unlucky, it get stuck in the ThreadPoolExecutore’s queue, or holds resource of ThreadPoolExecutore’s Thread. That’s the matter.
When the application is at a high traffic, every millisecond counts, which we don’t like to waste any. For example, with a thread pool, core size at 4, max size at 8, having a queue, size at 100.
Issue 1: Queueing
Task of User-8 may have enter the queue earlier than User-5’s, as a result, User-8’s task get chance to run earlier than User-5’s.
Issue 2: Time is Burning Out
When the play method is called, the clock is ticked in at the same moment, but its tasks will be executed by thread pool some time later. In another word, it may happen that play method finish work earlier than its tasks, cause tasks remain in the queue.
To sum up, tasks running or to-be-run beyond timeout of its calling method, have to be abondoned, or they will set others to wait, and others to waste.
We Clean Them
We clean tasks that have not run.
We clean tasks that shall not run.
Decorate task within a schedule task, while its timeout equals to play calling method, if this task finish up and call afterExecute schedule task get remove, or it will be interrupted by schedule task.