Class Service<V>
- Type Parameters:
-
V- the type of object returned by the Service
- All Implemented Interfaces:
-
Worker<V>,EventTarget
- Direct Known Subclasses:
ScheduledService
public abstract class Service<V> extends Object implements Worker<V>, EventTarget
A Service is a non-visual component encapsulating the information required to perform some work on one or more background threads. As part of the JavaFX UI library, the Service knows about the JavaFX Application thread and is designed to relieve the application developer from the burden of managing multithreaded code that interacts with the user interface. As such, all of the methods and state on the Service are intended to be invoked exclusively from the JavaFX Application thread. The only exception to this is when initially configuring a Service, which may safely be done from any thread, and initially starting a Service, which may also safely be done from any thread. However, once the Service has been initialized and started, it may only thereafter be used from the FX thread.
A Service creates and manages a Task that performs the work on the background thread. Service implements Worker. As such, you can observe the state of the background task and optionally cancel it. Service is a reusable Worker, meaning that it can be reset and restarted. Due to this, a Service can be constructed declaratively and restarted on demand. Once a Service is started, it will schedule its Task and listen for changes to the state of the Task. A Task does not hold a reference to the Service that started it, meaning that a running Task will not prevent the Service from being garbage collected.
If an Executor is specified on the Service, then it will be used to actually execute the service. Otherwise, a daemon thread will be created and executed. If you wish to create non-daemon threads, then specify a custom Executor (for example, you could use a ThreadPoolExecutor with a custom ThreadFactory).
Because a Service is intended to simplify declarative use cases, subclasses should expose as properties the input parameters to the work to be done. For example, to write a Service that reads the first line from any URL and returns it as a String, it might be defined with a single property, url, and might be implemented as:
public static class FirstLineService extends Service<String> {
private StringProperty url = new SimpleStringProperty(this, "url");
public final void setUrl(String value) { url.set(value); }
public final String getUrl() { return url.get(); }
public final StringProperty urlProperty() { return url; }
protected Task createTask() {
final String _url = getUrl();
return new Task<String>() {
protected String call() throws Exception {
URL u = new URL(_url);
BufferedReader in = new BufferedReader(
new InputStreamReader(u.openStream()));
String result = in.readLine();
in.close();
return result;
}
};
}
}
The Service by default uses a ThreadPoolExecutor with some unspecified default or maximum thread pool size. This is done so that naive code will not completely swamp the system by creating thousands of Threads.
- Since:
- JavaFX 2.0
Property Summary
| Type | Property | Description |
|---|---|---|
final ReadOnlyObjectProperty<Throwable> |
exception |
Gets the ReadOnlyObjectProperty representing any exception which occurred. |
final ObjectProperty<Executor> |
executor |
The executor to use for running this Service. |
final ReadOnlyStringProperty |
message |
Gets the ReadOnlyStringProperty representing the message. |
final ObjectProperty<EventHandler<WorkerStateEvent>> |
onCancelled |
The onCancelled event handler is called whenever the Task state transitions to the CANCELLED state. |
final ObjectProperty<EventHandler<WorkerStateEvent>> |
onFailed |
The onFailed event handler is called whenever the Task state transitions to the FAILED state. |
final ObjectProperty<EventHandler<WorkerStateEvent>> |
onReady |
The onReady event handler is called whenever the Task state transitions to the READY state. |
final ObjectProperty<EventHandler<WorkerStateEvent>> |
onRunning |
The onRunning event handler is called whenever the Task state transitions to the RUNNING state. |
final ObjectProperty<EventHandler<WorkerStateEvent>> |
onScheduled |
The onSchedule event handler is called whenever the Task state transitions to the SCHEDULED state. |
final ObjectProperty<EventHandler<WorkerStateEvent>> |
onSucceeded |
The onSucceeded event handler is called whenever the Task state transitions to the SUCCEEDED state. |
final ReadOnlyDoubleProperty |
progress |
Gets the ReadOnlyDoubleProperty representing the progress. |
final ReadOnlyBooleanProperty |
running |
Gets the ReadOnlyBooleanProperty representing whether the Worker is running. |
final ReadOnlyObjectProperty<Worker.State> |
state |
Gets the ReadOnlyObjectProperty representing the current state. |
final ReadOnlyStringProperty |
title |
Gets the ReadOnlyStringProperty representing the title. |
final ReadOnlyDoubleProperty |
totalWork |
Gets the ReadOnlyDoubleProperty representing the maximum amount of work that needs to be done. |
final ReadOnlyObjectProperty<V> |
value |
Gets the ReadOnlyObjectProperty representing the value. |
final ReadOnlyDoubleProperty |
workDone |
Gets the ReadOnlyDoubleProperty representing the current progress. |
Nested Class Summary
Nested classes/interfaces declared in interface javafx.concurrent.Worker
Worker.State
Constructor Summary
| Modifier | Constructor | Description |
|---|---|---|
protected |
Create a new Service. |
Method Summary
| Modifier and Type | Method | Description |
|---|---|---|
final <T extends Event> |
addEventFilter |
Registers an event filter for this target. |
final <T extends Event> |
addEventHandler |
Registers an event handler for this target. |
EventDispatchChain |
buildEventDispatchChain |
Construct an event dispatch chain for this target. |
boolean |
cancel() |
Cancels any currently running Task, if any. |
protected void |
cancelled() |
A protected convenience method for subclasses, called whenever the state of the Service has transitioned to the CANCELLED state. |
protected abstract Task<V> |
createTask() |
Invoked after the Service is started on the JavaFX Application Thread. |
final ReadOnlyObjectProperty<Throwable> |
exceptionProperty() |
Gets the ReadOnlyObjectProperty representing any exception which occurred. |
protected void |
executeTask |
Uses the executor defined on this Service to execute the given task. |
final ObjectProperty<Executor> |
executorProperty() |
The executor to use for running this Service. |
protected void |
failed() |
A protected convenience method for subclasses, called whenever the state of the Service has transitioned to the FAILED state. |
protected final void |
fireEvent |
Fires the specified event. |
final Throwable |
getException() |
Gets the value of the exception property. |
final Executor |
getExecutor() |
Gets the value of the executor property. |
final String |
getMessage() |
Gets the value of the message property. |
final EventHandler<WorkerStateEvent> |
getOnCancelled() |
The onCancelled event handler is called whenever the Task state transitions to the CANCELLED state. |
final EventHandler<WorkerStateEvent> |
getOnFailed() |
The onFailed event handler is called whenever the Task state transitions to the FAILED state. |
final EventHandler<WorkerStateEvent> |
getOnReady() |
The onReady event handler is called whenever the Task state transitions to the READY state. |
final EventHandler<WorkerStateEvent> |
getOnRunning() |
The onRunning event handler is called whenever the Task state transitions to the RUNNING state. |
final EventHandler<WorkerStateEvent> |
getOnScheduled() |
The onSchedule event handler is called whenever the Task state transitions to the SCHEDULED state. |
final EventHandler<WorkerStateEvent> |
getOnSucceeded() |
The onSucceeded event handler is called whenever the Task state transitions to the SUCCEEDED state. |
final double |
getProgress() |
Gets the value of the progress property. |
final Worker.State |
getState() |
Gets the value of the state property. |
final String |
getTitle() |
Gets the value of the title property. |
final double |
getTotalWork() |
Gets the value of the totalWork property. |
final V |
getValue() |
Gets the value of the value property. |
final double |
getWorkDone() |
Gets the value of the workDone property. |
final boolean |
isRunning() |
Gets the value of the running property. |
final ReadOnlyStringProperty |
messageProperty() |
Gets the ReadOnlyStringProperty representing the message. |
final ObjectProperty<EventHandler<WorkerStateEvent>> |
onCancelledProperty() |
The onCancelled event handler is called whenever the Task state transitions to the CANCELLED state. |
final ObjectProperty<EventHandler<WorkerStateEvent>> |
onFailedProperty() |
The onFailed event handler is called whenever the Task state transitions to the FAILED state. |
final ObjectProperty<EventHandler<WorkerStateEvent>> |
onReadyProperty() |
The onReady event handler is called whenever the Task state transitions to the READY state. |
final ObjectProperty<EventHandler<WorkerStateEvent>> |
onRunningProperty() |
The onRunning event handler is called whenever the Task state transitions to the RUNNING state. |
final ObjectProperty<EventHandler<WorkerStateEvent>> |
onScheduledProperty() |
The onSchedule event handler is called whenever the Task state transitions to the SCHEDULED state. |
final ObjectProperty<EventHandler<WorkerStateEvent>> |
onSucceededProperty() |
The onSucceeded event handler is called whenever the Task state transitions to the SUCCEEDED state. |
final ReadOnlyDoubleProperty |
progressProperty() |
Gets the ReadOnlyDoubleProperty representing the progress. |
protected void |
ready() |
A protected convenience method for subclasses, called whenever the state of the Service has transitioned to the READY state. |
final <T extends Event> |
removeEventFilter |
Unregisters a previously registered event filter from this target. |
final <T extends Event> |
removeEventHandler |
Unregisters a previously registered event handler from this target. |
void |
reset() |
Resets the Service. |
void |
restart() |
Cancels any currently running Task, if any, and restarts this Service. |
protected void |
running() |
A protected convenience method for subclasses, called whenever the state of the Service has transitioned to the RUNNING state. |
final ReadOnlyBooleanProperty |
runningProperty() |
Gets the ReadOnlyBooleanProperty representing whether the Worker is running. |
protected void |
scheduled() |
A protected convenience method for subclasses, called whenever the state of the Service has transitioned to the SCHEDULED state. |
protected final <T extends Event> |
setEventHandler |
Sets the handler to use for this event type. |
final void |
setExecutor |
Sets the value of the executor property. |
final void |
setOnCancelled |
The onCancelled event handler is called whenever the Task state transitions to the CANCELLED state. |
final void |
setOnFailed |
The onFailed event handler is called whenever the Task state transitions to the FAILED state. |
final void |
setOnReady |
The onReady event handler is called whenever the Task state transitions to the READY state. |
final void |
setOnRunning |
The onRunning event handler is called whenever the Task state transitions to the RUNNING state. |
final void |
setOnScheduled |
The onSchedule event handler is called whenever the Task state transitions to the SCHEDULED state. |
final void |
setOnSucceeded |
The onSucceeded event handler is called whenever the Task state transitions to the SUCCEEDED state. |
void |
start() |
Starts this Service. |
final ReadOnlyObjectProperty<Worker.State> |
stateProperty() |
Gets the ReadOnlyObjectProperty representing the current state. |
protected void |
succeeded() |
A protected convenience method for subclasses, called whenever the state of the Service has transitioned to the SUCCEEDED state. |
final ReadOnlyStringProperty |
titleProperty() |
Gets the ReadOnlyStringProperty representing the title. |
final ReadOnlyDoubleProperty |
totalWorkProperty() |
Gets the ReadOnlyDoubleProperty representing the maximum amount of work that needs to be done. |
final ReadOnlyObjectProperty<V> |
valueProperty() |
Gets the ReadOnlyObjectProperty representing the value. |
final ReadOnlyDoubleProperty |
workDoneProperty() |
Gets the ReadOnlyDoubleProperty representing the current progress. |
Property Details
state
public final ReadOnlyObjectProperty<Worker.State> stateProperty
- Specified by:
-
statePropertyin interfaceWorker<V> - Returns:
- The property representing the state
- See Also:
value
public final ReadOnlyObjectProperty<V> valueProperty
- Specified by:
-
valuePropertyin interfaceWorker<V> - Returns:
- The property representing the current value
- See Also:
exception
public final ReadOnlyObjectProperty<Throwable> exceptionProperty
- Specified by:
-
exceptionPropertyin interfaceWorker<V> - Returns:
- the property representing the exception
- See Also:
workDone
public final ReadOnlyDoubleProperty workDoneProperty
- Specified by:
-
workDonePropertyin interfaceWorker<V> - Returns:
- The property representing the amount of work done
- See Also:
totalWork
public final ReadOnlyDoubleProperty totalWorkProperty
- Specified by:
-
totalWorkPropertyin interfaceWorker<V> - Returns:
- the property representing the total work to be done
- See Also:
progress
public final ReadOnlyDoubleProperty progressProperty
- Specified by:
-
progressPropertyin interfaceWorker<V> - Returns:
- the property representing the progress
- See Also:
running
public final ReadOnlyBooleanProperty runningProperty
- Specified by:
-
runningPropertyin interfaceWorker<V> - Returns:
- the property representing whether the worker is running
- See Also:
message
public final ReadOnlyStringProperty messageProperty
- Specified by:
-
messagePropertyin interfaceWorker<V> - Returns:
- a property representing the current message
- See Also:
title
public final ReadOnlyStringProperty titleProperty
- Specified by:
-
titlePropertyin interfaceWorker<V> - Returns:
- the property representing the current title
- See Also:
executor
public final ObjectProperty<Executor> executorProperty
onReady
public final ObjectProperty<EventHandler<WorkerStateEvent>> onReadyProperty
- Since:
- JavaFX 2.1
- See Also:
onScheduled
public final ObjectProperty<EventHandler<WorkerStateEvent>> onScheduledProperty
- Since:
- JavaFX 2.1
- See Also:
onRunning
public final ObjectProperty<EventHandler<WorkerStateEvent>> onRunningProperty
- Since:
- JavaFX 2.1
- See Also:
onSucceeded
public final ObjectProperty<EventHandler<WorkerStateEvent>> onSucceededProperty
- Since:
- JavaFX 2.1
- See Also:
onCancelled
public final ObjectProperty<EventHandler<WorkerStateEvent>> onCancelledProperty
- Since:
- JavaFX 2.1
- See Also:
onFailed
public final ObjectProperty<EventHandler<WorkerStateEvent>> onFailedProperty
- Since:
- JavaFX 2.1
- See Also:
Constructor Details
Service
protected Service()
Method Details
getState
public final Worker.State getState()
state property.- Specified by:
-
getStatein interfaceWorker<V> - Property description:
- Returns:
- the value of the
stateproperty - See Also:
stateProperty
public final ReadOnlyObjectProperty<Worker.State> stateProperty()
Worker- Specified by:
-
statePropertyin interfaceWorker<V> - Returns:
- the
stateproperty - See Also:
getValue
public final V getValue()
value property.- Specified by:
-
getValuein interfaceWorker<V> - Property description:
- Returns:
- the value of the
valueproperty - See Also:
valueProperty
public final ReadOnlyObjectProperty<V> valueProperty()
Worker- Specified by:
-
valuePropertyin interfaceWorker<V> - Returns:
- the
valueproperty - See Also:
getException
public final Throwable getException()
exception property.- Specified by:
-
getExceptionin interfaceWorker<V> - Property description:
- Returns:
- the value of the
exceptionproperty - See Also:
exceptionProperty
public final ReadOnlyObjectProperty<Throwable> exceptionProperty()
Worker- Specified by:
-
exceptionPropertyin interfaceWorker<V> - Returns:
- the
exceptionproperty - See Also:
getWorkDone
public final double getWorkDone()
workDone property.- Specified by:
-
getWorkDonein interfaceWorker<V> - Property description:
- Returns:
- the value of the
workDoneproperty - See Also:
workDoneProperty
public final ReadOnlyDoubleProperty workDoneProperty()
Worker- Specified by:
-
workDonePropertyin interfaceWorker<V> - Returns:
- the
workDoneproperty - See Also:
getTotalWork
public final double getTotalWork()
totalWork property.- Specified by:
-
getTotalWorkin interfaceWorker<V> - Property description:
- Returns:
- the value of the
totalWorkproperty - See Also:
totalWorkProperty
public final ReadOnlyDoubleProperty totalWorkProperty()
Worker- Specified by:
-
totalWorkPropertyin interfaceWorker<V> - Returns:
- the
totalWorkproperty - See Also:
getProgress
public final double getProgress()
progress property.- Specified by:
-
getProgressin interfaceWorker<V> - Property description:
- Returns:
- the value of the
progressproperty - See Also:
progressProperty
public final ReadOnlyDoubleProperty progressProperty()
Worker- Specified by:
-
progressPropertyin interfaceWorker<V> - Returns:
- the
progressproperty - See Also:
isRunning
public final boolean isRunning()
running property.- Specified by:
-
isRunningin interfaceWorker<V> - Property description:
- Returns:
- the value of the
runningproperty - See Also:
runningProperty
public final ReadOnlyBooleanProperty runningProperty()
Worker- Specified by:
-
runningPropertyin interfaceWorker<V> - Returns:
- the
runningproperty - See Also:
getMessage
public final String getMessage()
message property.- Specified by:
-
getMessagein interfaceWorker<V> - Property description:
- Returns:
- the value of the
messageproperty - See Also:
messageProperty
public final ReadOnlyStringProperty messageProperty()
Worker- Specified by:
-
messagePropertyin interfaceWorker<V> - Returns:
- the
messageproperty - See Also:
getTitle
public final String getTitle()
title property.- Specified by:
-
getTitlein interfaceWorker<V> - Property description:
- Returns:
- the value of the
titleproperty - See Also:
titleProperty
public final ReadOnlyStringProperty titleProperty()
Worker- Specified by:
-
titlePropertyin interfaceWorker<V> - Returns:
- the
titleproperty - See Also:
setExecutor
public final void setExecutor(Executor value)
executor property.- Property description:
- The executor to use for running this Service. If no executor is specified, then a new daemon thread will be created and used for running the Service using some default executor.
- Parameters:
-
value- the value for theexecutorproperty - See Also:
getExecutor
public final Executor getExecutor()
executor property.- Property description:
- The executor to use for running this Service. If no executor is specified, then a new daemon thread will be created and used for running the Service using some default executor.
- Returns:
- the value of the
executorproperty - See Also:
executorProperty
public final ObjectProperty<Executor> executorProperty()
- Returns:
- the
executorproperty - See Also:
onReadyProperty
public final ObjectProperty<EventHandler<WorkerStateEvent>> onReadyProperty()
- Returns:
- the onReady event handler property
- Since:
- JavaFX 2.1
- See Also:
getOnReady
public final EventHandler<WorkerStateEvent> getOnReady()
- Returns:
- the onReady event handler, if any
- Since:
- JavaFX 2.1
setOnReady
public final void setOnReady(EventHandler<WorkerStateEvent> value)
- Parameters:
-
value- the event handler, can be null to clear it - Since:
- JavaFX 2.1
ready
protected void ready()
- Since:
- JavaFX 2.1
onScheduledProperty
public final ObjectProperty<EventHandler<WorkerStateEvent>> onScheduledProperty()
- Returns:
- the onScheduled event handler property
- Since:
- JavaFX 2.1
- See Also:
getOnScheduled
public final EventHandler<WorkerStateEvent> getOnScheduled()
- Returns:
- the onScheduled event handler, if any
- Since:
- JavaFX 2.1
setOnScheduled
public final void setOnScheduled(EventHandler<WorkerStateEvent> value)
- Parameters:
-
value- the event handler, can be null to clear it - Since:
- JavaFX 2.1
scheduled
protected void scheduled()
- Since:
- JavaFX 2.1
onRunningProperty
public final ObjectProperty<EventHandler<WorkerStateEvent>> onRunningProperty()
- Returns:
- the onRunning event handler property
- Since:
- JavaFX 2.1
- See Also:
getOnRunning
public final EventHandler<WorkerStateEvent> getOnRunning()
- Returns:
- the onRunning event handler, if any
- Since:
- JavaFX 2.1
setOnRunning
public final void setOnRunning(EventHandler<WorkerStateEvent> value)
- Parameters:
-
value- the event handler, can be null to clear it - Since:
- JavaFX 2.1
running
protected void running()
- Since:
- JavaFX 2.1
onSucceededProperty
public final ObjectProperty<EventHandler<WorkerStateEvent>> onSucceededProperty()
- Returns:
- the onSucceeded event handler property
- Since:
- JavaFX 2.1
- See Also:
getOnSucceeded
public final EventHandler<WorkerStateEvent> getOnSucceeded()
- Returns:
- the onSucceeded event handler, if any
- Since:
- JavaFX 2.1
setOnSucceeded
public final void setOnSucceeded(EventHandler<WorkerStateEvent> value)
- Parameters:
-
value- the event handler, can be null to clear it - Since:
- JavaFX 2.1
succeeded
protected void succeeded()
- Since:
- JavaFX 2.1
onCancelledProperty
public final ObjectProperty<EventHandler<WorkerStateEvent>> onCancelledProperty()
- Returns:
- the onCancelled event handler property
- Since:
- JavaFX 2.1
- See Also:
getOnCancelled
public final EventHandler<WorkerStateEvent> getOnCancelled()
- Returns:
- the onCancelled event handler, if any
- Since:
- JavaFX 2.1
setOnCancelled
public final void setOnCancelled(EventHandler<WorkerStateEvent> value)
- Parameters:
-
value- the event handler, can be null to clear it - Since:
- JavaFX 2.1
cancelled
protected void cancelled()
- Since:
- JavaFX 2.1
onFailedProperty
public final ObjectProperty<EventHandler<WorkerStateEvent>> onFailedProperty()
- Returns:
- the onFailed event handler property
- Since:
- JavaFX 2.1
- See Also:
getOnFailed
public final EventHandler<WorkerStateEvent> getOnFailed()
- Returns:
- the onFailed event handler, if any
- Since:
- JavaFX 2.1
setOnFailed
public final void setOnFailed(EventHandler<WorkerStateEvent> value)
- Parameters:
-
value- the event handler, can be null to clear it - Since:
- JavaFX 2.1
failed
protected void failed()
- Since:
- JavaFX 2.1
cancel
public boolean cancel()
- Specified by:
-
cancelin interfaceWorker<V> - Returns:
- returns true if the cancel was successful
restart
public void restart()
reset
public void reset()
start
public void start()
executeTask
protected void executeTask(Task<V> task)
Uses the executor defined on this Service to execute the given task. If the executor is null, then a default executor is used which will create a new daemon thread on which to execute this task.
This method is intended only to be called by the Service implementation.
- Parameters:
-
task- a non-null task to execute - Since:
- JavaFX 2.1
addEventHandler
public final <T extends Event> void addEventHandler(EventType<T> eventType, EventHandler<? super T> eventHandler)
The handler is called when the target receives an Event of the specified type during the bubbling phase of event delivery.
- Specified by:
-
addEventHandlerin interfaceEventTarget - Type Parameters:
-
T- the event class of the handler - Parameters:
-
eventType- the type of the events received by the handler -
eventHandler- the event handler - Since:
- JavaFX 2.1
removeEventHandler
public final <T extends Event> void removeEventHandler(EventType<T> eventType, EventHandler<? super T> eventHandler)
Since it is possible to register a single EventHandler instance for different event types, the caller needs to specify the event type from which the handler should be unregistered.
- Specified by:
-
removeEventHandlerin interfaceEventTarget - Type Parameters:
-
T- the event class of the handler - Parameters:
-
eventType- the event type from which to unregister -
eventHandler- the event handler - Since:
- JavaFX 2.1
addEventFilter
public final <T extends Event> void addEventFilter(EventType<T> eventType, EventHandler<? super T> eventFilter)
The filter is called when the target receives an Event of the specified type during the capturing phase of event delivery.
- Specified by:
-
addEventFilterin interfaceEventTarget - Type Parameters:
-
T- the event class of the filter - Parameters:
-
eventType- the type of the events received by the filter -
eventFilter- the event filter - Since:
- JavaFX 2.1
removeEventFilter
public final <T extends Event> void removeEventFilter(EventType<T> eventType, EventHandler<? super T> eventFilter)
Since it is possible to register a single EventHandler instance for different event types, the caller needs to specify the event type from which the filter should be unregistered.
- Specified by:
-
removeEventFilterin interfaceEventTarget - Type Parameters:
-
T- the event class of the filter - Parameters:
-
eventType- the event type from which to unregister -
eventFilter- the event filter - Since:
- JavaFX 2.1
setEventHandler
protected final <T extends Event> void setEventHandler(EventType<T> eventType, EventHandler<? super T> eventHandler)
- Type Parameters:
-
T- the specific event class of the handler - Parameters:
-
eventType- the event type to associate with the given eventHandler -
eventHandler- the handler to register, or null to unregister - Throws:
-
NullPointerException- if the event type is null - Since:
- JavaFX 2.1
fireEvent
protected final void fireEvent(Event event)
This method must be called on the FX user thread.
- Parameters:
-
event- the event to fire - Since:
- JavaFX 2.1
buildEventDispatchChain
public EventDispatchChain buildEventDispatchChain(EventDispatchChain tail)
EventTargetEventTarget. This event target is not automatically added to the chain, so if it wants to process events, it needs to add an EventDispatcher for itself to the chain. In the case the event target is part of some hierarchy, the chain for it is usually built from event dispatchers collected from the root of the hierarchy to the event target.
The event dispatch chain is constructed by modifications to the provided initial event dispatch chain. The returned chain should have the initial chain at its end so the dispatchers should be prepended to the initial chain.
The caller shouldn't assume that the initial chain remains unchanged nor that the returned value will reference a different chain.
- Specified by:
-
buildEventDispatchChainin interfaceEventTarget - Parameters:
-
tail- the initial chain to build from - Returns:
- the resulting event dispatch chain for this target
createTask
protected abstract Task<V> createTask()
protected Task createTask() {
final String url = myService.getUrl();
return new Task<String>() {
protected String call() {
URL u = new URL("http://www.oracle.com");
BufferedReader in = new BufferedReader(
new InputStreamReader(u.openStream()));
String result = in.readLine();
in.close();
return result;
}
}
}
If the Task is a pre-defined class (as opposed to being an anonymous class), and if it followed the recommended best-practice, then there is no need to save off state prior to constructing the Task since its state is completely provided in its constructor.
protected Task createTask() {
// This is safe because getUrl is called on the FX Application
// Thread and the FirstLineReaderTasks stores it as an
// immutable property
return new FirstLineReaderTask(myService.getUrl());
}
- Returns:
- the Task to execute
© 2008, 2025, Oracle and/or its affiliates. All rights reserved.
Documentation extracted from JavaFX API Documentation.
Licensed under the GNU General Public License, version 2, with the Classpath Exception.
Java, JavaFX and OpenJDK are trademarks or registered trademarks of Oracle and/or its affiliates.