Java Reference
In-Depth Information
Listing 9.5. Long-running Task with User Feedback.
The task triggered when the button is pressed is composed of three sequential subtasks whose
execution alternates between the event thread and the background thread. The first subtask
updates the user interface to show that a longrunning operation has begun and starts the
second subtask in a background thread. Upon completion, the second subtask queues the third
subtask to run again in the event thread, which updates the user interface to reflect that the
operation has completed. This sort of “thread hopping” is typical of handling long-running
tasks in GUI applications.
9.3.1. Cancellation
Any task that takes long enough to run in another thread probably also takes long enough that
the user might want to cancel it. You could implement cancellation directly using thread in-
terruption, but it is much easier to use Future , which was designed to manage cancellable
tasks.
Search WWH ::




Custom Search