Java Reference
In-Depth Information
Understanding the
Worker
Interface
The
Worker
interface specifies a JavaFX bean with nine read-only properties, one method named
cancel()
, and a
state model and state transition rules. A
Worker
represents a unit of work that runs in one or more background threads
yet has some of its internal states safely observable to the JavaFX application thread. The nine read-only properties are
as follows.
title
is a
String
property that represents the title of the task.
•
message
is a
String
property that represents a more detailed message as the task progresses.
•
running
is a
boolean
property that is true only when the
Worker
is in the
Worker.State.SCHEDULED
or
Worker.State.RUNNING
state.
•
state
is an
Object
property that represents the
Worker.State
of the task.
•
totalWork
is a
double
property that represents the total amount of work of the task. Its value is
-1.0
when the total amount of work is not known.
•
workDone
is a
double
property that represents the amount of work that has been done so far in
the task. Its value is
-1.0
or a number between
0
and
totalWork
.
•
progress
is a
double
property that represents the percentage of the total work that has been
done so far in the task. Its value is
-1.0
or the ratio between
workDone
and
totalWork
.
•
value
is an
Object
property that represents the output of the task. Its value is non-null only
when the task has finished successfully, that is, has reached the
Worker.State.SUCCEEDED
state.
•
exception
is an
Object
property that represents a
Throwable
that the implementation of the
task has thrown to the JavaFX worker threading framework. Its value is non-null only when the
task is in the
Worker.State.FAILED
state.
•
The preceding properties are meant to be accessed from the JavaFX application thread. It is safe to bind scene
graph properties to them because the invalidation events and change events of these properties are fired on the JavaFX
application thread. It is helpful to think of the properties through an imaginary task progress message box that you see
in many GUI applications. They usually have a title, a progress bar indicating the percentage of the work that has been
done, and a message telling the user how many items it has processed already and how many more to go. All of these
properties are set by the JavaFX worker threading framework itself or by the actual implementation of the task.
The
running
,
state
,
value
, and
exception
properties are controlled by the framework and no user intervention
is needed for them to be observed in the JavaFX application thread. When the framework wants to change these
properties, it does the heavy lifting of making sure that the change is done on the JavaFX application thread. The
title
,
message
,
totalWork
,
workDone
, and
progress
properties are updatable by the implementation code of the task
by calling framework-provided protected methods that do the heavy lifting of making sure that the change is done on
the JavaFX application thread.
Worker.State
is a nested enum that defines the following six states of a
Worker
:
READY (initial state)
•
SCHEDULED (transitional state)
•
RUNNING (transitional state)
•
SUCCEEDED (terminal state)
•
CANCELLED (terminal state)
•
FAILED (terminal state)
•
Search WWH ::
Custom Search