Java Reference
In-Depth Information
The cancel() method will transition the Worker to the CANCELLED state if it is not already in the SUCCEEDED or
FAILED state.
Now that you are familiar with the properties and states of the Worker interface, you can proceed to learn
the three abstract classes in the JavaFX worker threading framework that implements this interface, Task<V> and
Service<V> , and ScheduledService<V> .
Understanding the Task<V> Abstract Class
The Task<V> abstract class is an implementation of the Worker interface that is meant to be used for one-shot
tasks. Once its state progresses to SUCCEEDED or FAILED or CANCELLED , it will stay in the terminal state forever. The
Task<V> abstract class extends the FutureTask<V> class, and as a consequence supports the Runnable , Future<V> ,
and RunnableFuture<V> interfaces as well as the Worker interface. The Future<V> , RunnableFuture<V> , and
FutureTask<V> interfaces and class are part of the java.util.concurrent package. Because of this heritage, a
Task<V> object can be used in various ways that befit its parent class. However, for typical JavaFX usage, it is enough
to use just the methods in the Task<V> class itself, a list of which can be found in the Javadoc for the class. Here is a
listing of these methods, excluding the read-only properties that were discussed in the preceding section:
protected abstract V call() throws Exception
public final boolean cancel()
public boolean cancel(boolean mayInterruptIfRunning)
protected void updateTitle(String title)
protected void updateMessage(String message)
protected void updateProgress(long workDone, long totalWork)
protected void updateProgress(double workDone, double totalWork)
protected void updateValue(V)
The Task<V> abstract class implements the javafx.event.EventTarget interface. The events it supports
are represented by the WorkerStateEvent class. There is a WorkerStateEvent for each of the five Worker.States .
The events are fired when the Task<V> transitions into a state. There are five object properties of type
EventHandler<WorkerStateEvent> as well as five protected methods in Task<V> . These event handlers and protected
methods are called when the corresponding event is fired:
onScheduled property
onRunning property
onSucceeded property
onCancelled property
onFailed property
protected void scheduled()
protected void running()
protected void succeeded()
protected void cancelled()
protected void failed()
 
Search WWH ::




Custom Search