/* * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved. * ORACLE PROPRIETARY/CONFIDENTIAL. Use is subject to license terms. * * * * * * * * * * * * * * * * * * * * */ package javafx.concurrent; import javafx.beans.property.ReadOnlyBooleanProperty; import javafx.beans.property.ReadOnlyDoubleProperty; import javafx.beans.property.ReadOnlyObjectProperty; import javafx.beans.property.ReadOnlyStringProperty; /** *
* A Worker is an object which performs some work in one or more background * threads, and who's state is observable and available to JavaFX applications * and is usable from the main JavaFX Application thread. This interface is * primarily implemented by both {@link Task} and {@link Service}, providing * a common API among both classes which makes it easier for libraries and * frameworks to provide workers which work well when developing user interfaces. *
** A Worker may or may not be reusable depending on the implementation. A * {@link Task}, for example, is not reusable while a {@link Service} is. *
** A Worker has a well defined life cycle. Every Worker begins in the * {@link State#READY} state. When the Worker has been scheduled for work * (for example, when a Service's {@link javafx.concurrent.Service#start()} * method is called), it is transitioned to {@link State#SCHEDULED}. Even * Workers which are not technically scheduled, but started immediately * (such as with {@link javafx.concurrent.Task#run()}) will transition through * the {@link State#SCHEDULED} on its way to the {@link State#RUNNING} state. *
*
* When the Worker is actually performing its work, the state will have been
* transitioned to {@link State#RUNNING}. If the Worker completes normally,
* it will end in the {@link State#SUCCEEDED} state, and the result of the
* Worker will be set as the value
property. If however an Exception
* occurs during the execution of the Worker, then the state will be set to
* {@link State#FAILED} and the exception
property will be set
* to the Exception which occurred.
*
* At any time prior to the conclusion of the Worker (that is, if the state * is not already {@link State#SUCCEEDED} or {@link State#FAILED}) the developer * may invoke the {@link javafx.concurrent.Worker#cancel()} method. If called, the * Worker will cease execution (if possible, including use of Thread.interrupt) * and the state changed to {@link State#CANCELLED}. *
*
* The only valid beginning state for a Worker is {@link State#READY}, and the
* valid ending states are {@link State#CANCELLED}, {@link State#SUCCEEDED},
* and {@link State#FAILED}. The running
property is set to
* true when the state is either {@link State#SCHEDULED} or {@link State#RUNNING}.
*
* The Worker's progress can be monitored via three different properties,
* totalWork
, workDone
, and progress
.
* These properties are set by the actual implementation of the Worker
* interface, but can be observed by anybody. The workDone
is
* a number between -1 (meaning indeterminate progress) and
* totalWork
, inclusive. When workDone == totalWork
* the progress
will be 100% (or 1). totalWork
* will be a number between -1 and Long.MAX_VALUE, inclusive. The progress
* will be either -1 (meaning indeterminate), or a value between 0 and 1, inclusive,
* representing 0% through 100%.
*
* A Worker which is in the {@link State#READY} or {@link State#SCHEDULED} states
* will always have workDone
and progress
set to -1.
* A Worker which is in the {@link State#SUCCEEDED} state will always have
* workDone == totalWork
and progress == 1
. In any
* other state, the values for these properties may be any value in their
* respective valid ranges.
*
* The state of a Worker. The state transitions in a Worker are very well defined. * All Workers begin in the READY state. In some circumstances, a Worker might * be scheduled for execution before it is actually executed. In such cases, * it is sometimes useful to know when the Worker has been SCHEDULED separately * from when it is RUNNING. However even in cases where the Worker is executed * immediately, the Worker will temporarily enter the SCHEDULED state before * entering the RUNNING state. That is, the transition is always from * READY to SCHEDULED to RUNNING (unless of course the Worker in cancelled). *
** A Worker which runs but is never cancelled can only end up in one of two * states, either SUCCEEDED or FAILED. It only enters FAILED if an exception * was thrown during the execution of the Worker. A Worker may be cancelled when * READY, SCHEDULED, or RUNNING, in which case the final status will be CANCELLED. * When a Worker is cancelled in one of these circumstances it will transition * immediately to the CANCELLED state. *
** A reusable Worker will transition from CANCELLED, SUCCEEDED or FAILED back to * READY. From that point the normal state transitions are again followed. *
* @since JavaFX 2.0 */ public enum State { /** * Indicates that the Worker has not yet been executed and is ready * to be executed, or that it has been reinitialized. This is the * default initial state of the Worker. */ READY, /** * Indicates that the Worker has been scheduled for execution, but * that it is not currently running. This might be because the * Worker is waiting for a thread in a thread pool to become * available before it can start running. */ SCHEDULED, /** * Indicates that this Worker is running. This is set just immediately * prior to the Worker actually doing its first bit of work. */ RUNNING, /** * Indicates that this Worker has completed successfully, and that there * is a valid result ready to be read from thevalue
property.
*/
SUCCEEDED,
/**
* Indicates that this Worker has been cancelled via the {@link #cancel()}
* method.
*/
CANCELLED,
/**
* Indicates that this Worker has failed, usually due to some unexpected
* condition having occurred. The exception can be retrieved from the
* exception
property.
*/
FAILED
}
/**
* Specifies the current state of this Worker. The initial value is State.READY.
* A Task may be restarted, in which case it will progress from one of these
* end states (SUCCEEDED, CANCELLED, or FAILED) back to READY and then
* immediately to SCHEDULED and RUNNING. These state transitions may occur
* immediately one after the other, but will always occur in the prescribed order.
*
* @return The current state of this Worker
*/
public State getState();
/**
* Gets the ReadOnlyObjectProperty representing the current state.
*
* @return The property representing the state
*/
public ReadOnlyObjectProperty