Java Reference
In-Depth Information
Figure 7-11.
The
WorkerAndTaskExample
program after the task threw an exception
We overrode the
scheduled()
and
running()
methods in the anonymous extension of
Task
, and set up event
handlers for the succeeded, cancelled, and failed events, in the
Model
class. You should see these events being logged
onto the console as you work through the scenarios.
■
the
Task<V>
class defines one-shot tasks that are executed once and never run again. you have to restart the
WorkerAndTaskExample
program after each run of the task.
Note
Understanding the
Service<V>
Abstract Class
The
Service<V>
abstract class is an implementation of the
Worker
interface that is meant to be reused. It extends
Worker
's state model by allowing its state to be reset to
Worker.State.READY
. The
Service<V>
abstract class does not
extend any class and implements the
Worker
and
EventTarget
interfaces. In addition to the nine read-only properties
of the
Worker
interface,
Service<V>
has an additional read write property of type
Executor
called
executor
. It also has
the event handler properties and the protected event callback methods just like the
Task
class. Here is a listing of the
rest of the methods of
Service<V>
:
protected abstract Task<V> createTask()
•
public void start()
•
public void reset()
•
public void restart()
•
public boolean cancel()
Extensions of
Service<V>
must override the protected abstract
createTask()
method to generate a freshly
created
Task
. The
start()
method can only be called when the
Service<V>
object is in the
Worker.State.READY
state. It calls
createTask()
to obtain a freshly minted
Task
, and asks the
executor
property for an
Executor
. If the
executor
property is not set, it creates its own
Executor
. It binds the
Service<V>
object's nine
Worker
properties
to that of the
Task
's. It then transitions the
Task
to the
Worker.State.SCHEDULED
state, and executes the
Task
on
•
Search WWH ::
Custom Search