Spec-Zone .ru
спецификации, руководства, описания, API
|
public abstract class Task<V> extends java.util.concurrent.FutureTask<V> implements Worker<V>
A fully observable implementation of a FutureTask
. Tasks exposes
additional state and observable properties useful for programming asynchronous
tasks in JavaFX, as defined in the Worker
interface. An implementation
of Task must override the call()
method. This method
is invoked on the background thread. Any state which is used in this method
must be safe to read and write from a background thread. For example, manipulating
a live scene graph from this method is unsafe and will result in runtime
exceptions.
Tasks are flexible and extremely useful for the encapsulation of "work". Because
Service
is designed to execute a Task, any Tasks defined by the application
or library code can easily be used with a Service. Likewise, since Task extends
from FutureTask, it is very easy and natural to use a Task with the java concurrency
Executor
API. Finally, since a Task is Runnable, you
can also call it directly (by invoking the FutureTask.run()
method)
from another background thread. This allows for composition of work, or pass it to
a new Thread constructed and executed manually.
Although ExecutorService
defines several methods which
take a Runnable, you should generally limit yourself to using the execute
method inherited from Executor
.
As with FutureTask, a Task is a one-shot class and cannot be reused. See Service
for a reusable Worker
.
Because the Task is designed for use with JavaFX GUI applications, it ensures
that every change to its public properties, as well as change notifications
for state, errors, and for event handlers, all occur on the main JavaFX application
thread. Accessing these properties from a background thread (including the
call()
method) will result in runtime exceptions being raised.
It is strongly encouraged that all Tasks be initialized with immutable state upon which the Task will operate. This should be done by providing a Task constructor which takes the parameters necessary for execution of the Task. Immutable state makes it easy and safe to use from any thread and ensures correctness in the presence of multiple threads.
Type | Property and Description |
---|---|
ReadOnlyObjectProperty<java.lang.Throwable> |
exception
Gets the ReadOnlyObjectProperty representing any exception which occurred.
|
ReadOnlyStringProperty |
message
Gets the ReadOnlyStringProperty representing the message.
|
ReadOnlyDoubleProperty |
progress
Gets the ReadOnlyLongProperty representing the progress.
|
ReadOnlyBooleanProperty |
running
Gets the ReadOnlyBooleanProperty representing whether the Worker is running.
|
ReadOnlyObjectProperty<Worker.State> |
state
Gets the ReadOnlyObjectProperty representing the current state.
|
ReadOnlyStringProperty |
title
Gets the ReadOnlyStringProperty representing the title.
|
ReadOnlyDoubleProperty |
totalWork
Gets the ReadOnlyLongProperty representing the maximum amount of work
that needs to be done.
|
ReadOnlyObjectProperty<V> |
value
Gets the ReadOnlyObjectProperty representing the value.
|
ReadOnlyDoubleProperty |
workDone
Gets the ReadOnlyLongProperty representing the current progress.
|
Worker.State
Constructor and Description |
---|
Task()
Creates a new Task.
|
Modifier and Type | Method and Description |
---|---|
protected abstract V |
call()
Invoked when the Task is executed, the call method must be overridden and
implemented by subclasses.
|
boolean |
cancel()
Terminates execution of this Worker.
|
boolean |
cancel(boolean mayInterruptIfRunning) |
ReadOnlyObjectProperty<java.lang.Throwable> |
exceptionProperty()
Gets the ReadOnlyObjectProperty representing any exception which occurred.
|
java.lang.Throwable |
getException()
Indicates the exception which occurred while the Worker was running, if any.
|
java.lang.String |
getMessage()
Gets a message associated with the current state of this Worker.
|
double |
getProgress()
Indicates the current progress of this Worker in terms of percent complete.
|
Worker.State |
getState()
Specifies the current state of this Worker.
|
java.lang.String |
getTitle()
An optional title that should be associated with this Worker.
|
double |
getTotalWork()
Indicates a maximum value for the
Worker.workDoneProperty() property. |
V |
getValue()
Specifies the value, or result, of this Worker.
|
double |
getWorkDone()
Indicates the current amount of work that has been completed.
|
boolean |
isRunning()
True if the state is either SCHEDULED or RUNNING.
|
ReadOnlyStringProperty |
messageProperty()
Gets the ReadOnlyStringProperty representing the message.
|
ReadOnlyDoubleProperty |
progressProperty()
Gets the ReadOnlyLongProperty representing the progress.
|
ReadOnlyBooleanProperty |
runningProperty()
Gets the ReadOnlyBooleanProperty representing whether the Worker is running.
|
ReadOnlyObjectProperty<Worker.State> |
stateProperty()
Gets the ReadOnlyObjectProperty representing the current state.
|
ReadOnlyStringProperty |
titleProperty()
Gets the ReadOnlyStringProperty representing the title.
|
ReadOnlyDoubleProperty |
totalWorkProperty()
Gets the ReadOnlyLongProperty representing the maximum amount of work
that needs to be done.
|
protected void |
updateMessage(java.lang.String message)
Updates the
message property. |
protected void |
updateProgress(long workDone,
long max)
Updates the
workDone , totalWork ,
and progress properties. |
protected void |
updateTitle(java.lang.String title)
Updates the
title property. |
ReadOnlyObjectProperty<V> |
valueProperty()
Gets the ReadOnlyObjectProperty representing the value.
|
ReadOnlyDoubleProperty |
workDoneProperty()
Gets the ReadOnlyLongProperty representing the current progress.
|
stateProperty
in interface Worker<V>
getState()
valueProperty
in interface Worker<V>
getValue()
exceptionProperty
in interface Worker<V>
getException()
workDoneProperty
in interface Worker<V>
getWorkDone()
totalWorkProperty
in interface Worker<V>
getTotalWork()
progressProperty
in interface Worker<V>
getProgress()
runningProperty
in interface Worker<V>
isRunning()
messageProperty
in interface Worker<V>
getMessage()
titleProperty
in interface Worker<V>
getTitle()
protected abstract V call() throws java.lang.Exception
java.lang.Exception
public final Worker.State getState()
Worker
public final ReadOnlyObjectProperty<Worker.State> stateProperty()
Worker
stateProperty
in interface Worker<V>
getState()
public final V getValue()
Worker
public final ReadOnlyObjectProperty<V> valueProperty()
Worker
valueProperty
in interface Worker<V>
getValue()
public final java.lang.Throwable getException()
Worker
null
, there is no known exception, even if
the status is FAILED. If this property is not null
, it will most
likely contain an exception that describes the cause of failure.getException
in interface Worker<V>
public final ReadOnlyObjectProperty<java.lang.Throwable> exceptionProperty()
Worker
exceptionProperty
in interface Worker<V>
getException()
public final double getWorkDone()
Worker
getWorkDone
in interface Worker<V>
Worker.totalWorkProperty()
,
Worker.progressProperty()
public final ReadOnlyDoubleProperty workDoneProperty()
Worker
workDoneProperty
in interface Worker<V>
getWorkDone()
public final double getTotalWork()
Worker
Worker.workDoneProperty()
property. The
totalWork will either be -1 (indicating that the amount of work
to do is indeterminate), or it will be a non-zero value less than or
equal to Long.MAX_VALUE.getTotalWork
in interface Worker<V>
Worker.workDoneProperty()
,
Worker.progressProperty()
public final ReadOnlyDoubleProperty totalWorkProperty()
Worker
totalWorkProperty
in interface Worker<V>
getTotalWork()
public final double getProgress()
Worker
getProgress
in interface Worker<V>
Worker.workDoneProperty()
,
Worker.totalWorkProperty()
public final ReadOnlyDoubleProperty progressProperty()
Worker
progressProperty
in interface Worker<V>
getProgress()
public final boolean isRunning()
Worker
ProgressIndicator
, you will typically bind the visibility
of the ProgressIndicator to the Worker's running property, and the progress of the
ProgressIndicator to the Worker's progress property.public final ReadOnlyBooleanProperty runningProperty()
Worker
runningProperty
in interface Worker<V>
isRunning()
public final java.lang.String getMessage()
Worker
getMessage
in interface Worker<V>
public final ReadOnlyStringProperty messageProperty()
Worker
messageProperty
in interface Worker<V>
getMessage()
public final java.lang.String getTitle()
Worker
public final ReadOnlyStringProperty titleProperty()
Worker
titleProperty
in interface Worker<V>
getTitle()
public final boolean cancel()
Worker
public boolean cancel(boolean mayInterruptIfRunning)
protected void updateProgress(long workDone, long max)
workDone
, totalWork
,
and progress
properties. Calls to updateProgress
are coalesced and run later on the FX application thread, and calls
to updateProgress, even from the FX Application thread, may not
necessarily result in immediate updates to these properties, and
intermediate workDone values may be coalesced to save on event
notifications. max
becomes the new value for
totalWork
.
This method is safe to be called from any thread.
workDone
- A value from -1 up to max. If the value is greater
than max, an illegal argument exception is thrown.
If the value passed is -1, then the resulting percent
done will be -1 (thus, indeterminate).max
- A value from -1 to Long.MAX_VALUE. Any value outside this
range results in an IllegalArgumentException.protected void updateMessage(java.lang.String message)
message
property. Calls to updateMessage
are coalesced and run later on the FX application thread, so calls
to updateMessage, even from the FX Application thread, may not
necessarily result in immediate updates to this property, and
intermediate message values may be coalesced to save on event
notifications.
This method is safe to be called from any thread.
message
- the new messageprotected void updateTitle(java.lang.String title)
title
property. Calls to updateTitle
are coalesced and run later on the FX application thread, so calls
to updateTitle, even from the FX Application thread, may not
necessarily result in immediate updates to this property, and
intermediate title values may be coalesced to save on event
notifications.
This method is safe to be called from any thread.
title
- the new titleCopyright (c) 2008, 2011, Oracle and/or its affiliates. All rights reserved. Use is subject to