ScheduledExecutorService workflows retain interrupted status after FutureTask.cancel (true)

I have a task that I plan to run periodically through ScheduledThreadPoolExecutor.scheduleAtFixedRate (task, speed, ...) . The user can cancel this task manually, which calls ScheduledFuture.cancel (true) . For some reason, perhaps depending on when they canceled this task, the workflow (which the executor used to complete my task) apparently remained in an interrupted state after my run () method exited.

I would like workflows (taken from the pool and reused) to delete their aborted status before starting a new task using existing hooks (via ThreadPoolExecutor.beforeExecute () or ThreadPoolExecutor.afterExecute () ). But this is not performed in the default implementation.

I have two questions:

  • How does a workflow work in the state in which the interrupt status is set?
  • Why doesn't the default implementation clear the interrupt status before starting a new task?
+4
source share
2 answers
* How is it that the worker thread is left in a state where the interrupt status is set? * Why does the default implementation not clear the interrupt status before starting a new task? 

Answers:

  • He does not remain in an interrupted state.
  • The implementation is in progress, but you are not looking at the right place.

From the Oracle library code:

  /* * Ensure that unless pool is stopping, this thread * does not have its interrupt set. This requires a * double-check of state in case the interrupt was * cleared concurrently with a shutdownNow -- if so, * the interrupt is re-enabled. */ if (runState < STOP && Thread.interrupted() && runState >= STOP) thread.interrupt(); 

As you can see, the interrupt status is cleared from the workflow until the executor disconnects.

+5
source

How does a user interrupt a thread? If they use the user interface component, the problem may be related to synchronization problems with the event dispatch flow.

-1
source

All Articles