How does Executor.newSingleThreadExecutor()
behave if I am frequently scheduling tasks to run that are being cancelled with future.cancel(true);
?
Does the single thread spawned by the executor get interrupted (so the future code needs to clear the interrupt), or does the interrupt flag get automatically cleared when the next future starts up.
Does the Executor need to spawn an additional thread on every interrupt to be used by the remaining task queue?
Is there a better way?
Good question, I don't find this documented anywhere, so I would say it is implementation dependent.
For example OpenJDK does reset the interrupted flag before every executed task:
Snippet from from OpenJDK jdk8u ThreadPoolExecutor#runWorker source.