Profile cover photo
Profile photo
Rafał Piotrowicz
Rafał's posts

Post has shared content
This is an article about the JavaFX Concurrent Framework API. Java 5 added a comprehensive concurrency framework to the Java programming language through the libraries in the java.util.concurrent package. The JavaFX Concurrency Framework is very small.
It is built on top of the Java language Concurrency Framework keeping in mind that it will be used in a GUI environment.
The following table shows an overview of the whole article:

Table Of Contents

1. Introduction
2. Understanding the Worker Interface

2.1 Utility Classes

2.2 State Transitions for a Worker

2.3 Properties of a Worker

3. Using the Task Class

3.1 The Code

3.2 Creating a Task

3.3 Updating Task Properties

3.4 Listening to Task Transition Events

3.5 Cancelling a Task

3.6 Running a Task

3.7 The GUI

4. Using the Service Class

4.1 The Code

4.2 Creating a Service

4.3 Updating Service Properties

4.4 Cancelling the Service

4.5 Starting the Service

4.6 Resetting the Service

4.7 Restarting the Service

4.8 The GUI

5. Using the ScheduledService Class

5.1 The Code

5.2 Creating a ScheduledService

5.3 Updating ScheduledService Properties

5.4 Listening to ScheduledService Transition Events

5.5 The GUI

6. Download Java Source Code

The following examples use Java SE 8 and JavaFX 2.2.
1. Introduction
The framework consists of one interface, four classes, and one enum.
An instance of the Worker interface represents a Task that needs to be performed in one or more background threads. The state of the Task is observable from the JavaFX Application Thread.
The Task, Service, and ScheduledService classes implement the Worker interface. They represent different types of tasks. They are abstract classes. An instance of the Task class represents a one-shot task.
A Task cannot be reused. An instance of the Service class represents a reusable task. The ScheduledService class inherits from the Service class. A ScheduledService is a Task that can be scheduled to run repeatedly after a specified interval.
The constants in the Worker.State enum represent different states of a Worker.
An instance of the WorkerStateEvent class represents an event that occurs as the state of a Worker changes. You can add event handlers to all three types of tasks to listen to the change in their states.
2. Understanding the Worker Interface
The Worker interface provides the specification for any task performed by the JavaFX Concurrency Framework. A Worker is a Task that is performed in one or more background threads. The generic parameter V is the data type of the result of the Worker.
The state of the Task is observable. The state of the Task is published on the JavaFX Application Thread, making it possible for the Task to communicate with the Scene Graph, as is commonly required in a GUI application.
2.1 Utility Classes
Let us create the reusable GUI and non-GUI parts of the programs to use in examples in the following sections.
The WorkerStateGUI class builds a GridPane to display all properties of a Worker.
It is used with a Worker. It displays the properties of a Worker by UI elements to them. You can bind properties of a Worker to the UI elements by passing a Worker to the constructor or calling the bindToWorker() method.
import javafx.beans.binding.When;
import javafx.collections.ObservableList;
import javafx.concurrent.Worker;
import javafx.scene.control.Label;
import javafx.scene.control.ProgressBar;
import javafx.scene.control.TextArea;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.HBox;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;

public class WorkerStateGUI extends GridPane

// Create the Labels
private final Label title = new Label("");
private final Label message = new Label("");
private final Label running = new Label("");
private final Label state = new Label("");
private final Label totalWork = new Label("");
private final Label workDone = new Label("");
private final Label progress = new Label("");

// Create the TextAreas
private final TextArea value = new TextArea("");
private final TextArea exception = new TextArea("");

// Create the ProgressBar
private final ProgressBar progressBar = new ProgressBar();

public WorkerStateGUI()


public WorkerStateGUI(Worker worker)


private void addGUI()



addRow(0, new Label("Title:"), title);
addRow(1, new Label("Message:"), message);
addRow(2, new Label("Running:"), running);
addRow(3, new Label("State:"), state);
addRow(4, new Label("Total Work:"), totalWork);
addRow(5, new Label("Work Done:"), workDone);
addRow(6, new Label("Progress:"), new HBox(2, progressBar, progress));
addRow(7, new Label("Value:"), value);
addRow(8, new Label("Exception:"), exception);

public void bindToWorker(final Worker worker)

// Bind Labels to the properties of the worker

new When(worker.totalWorkProperty().isEqualTo(-1)).then("Unknown")

new When(worker.workDoneProperty().isEqualTo(-1)).then("Unknown")

new When(worker.progressProperty().isEqualTo(-1)).then("Unknown")


worker.exceptionProperty().addListener(new ChangeListener()

public void changed(ObservableValue

Post has attachment

Post has attachment

Post has attachment

Post has attachment

Post has attachment

Post has attachment

Post has attachment

Post has attachment

Post has attachment
Wait while more posts are being loaded