Java Reference
In-Depth Information
When you call cancel on a Future with mayInterruptIfRunning set to true , the
Future implementation interrupts the thread that is executing the task if it is currently run-
ning. If your task is written to be responsive to interruption, it can return early if it is can-
celled. Listing 9.6 illustrates a task that polls the thread's interrupted status and returns early
on interruption.
Listing 9.6. Cancelling a Long-running Task.
Because runningTask is confined to the event thread, no synchronization is required when
setting or checking it, and the start button listener ensures that only one background task is
running at a time. However, it would be better to be notified when the task completes so that,
for example, the cancel button could be disabled. We address this in the next section.
Search WWH ::




Custom Search