JavaFX AsyncTask: A easy way to handle multithreading in JavaFX

Victor Oliveira
Aug 26, 2017 · 3 min read

This post is a duplicate of my original blog. The purpose of this post is entirely to keep alive some posts from my old blog.


My first professional application was a JavaFX app that was supposed to communicate with a remote server and perform normal operations such as login, requests, file downloads, and so on. That’s when I started to discover thread’s world and how complicated it can be.

In web applications it is very common that most async operations are controlled by the application container server (concurrent requisitions, etc). What happens in desktop and mobile applications is different, you must manage all your threads including the User Interface thread.

In android for example, it is forbidden to perform web request operations on the main thread because the latency of the network, may cause the main thread to freeze while waiting for a response, and the application stops responding. For this, Android Platform has a class called AsyncTask that must be inherited and its three main methods implemented to abstract the thread management problem.

After experiencing some similar problems in my JavaFX applications (you can see in Stack Overflow here (Stop threads before close my JavaFX program), here (Stop threads before close my JavaFX program) and here (ProgressIndicator freezes when button is pressed. JavaFX)) I decided to implement something similar to what android made available for JavaFX.


JavaFX AsyncTask

This class was created to simplify how to handle Thread tasks in JavaFX, and it is based on the same idea of AsyncTask from Android.

An asynchronous task is defined by a computation that runs on a background thread and whose result is published on the UI thread. An asynchronous task is defined by extending the class, and 4 steps, called onPreExecute, doInBackground, progressCallback and onPostExecute.

Optionally you have the method setDaemon to set your threads daemon, which means that if your JavaFX application has been closed it can still running or not. setDamon(boolean) can only be called before the thread has been started. By default the thread is set to daemon.

Methods

doInBackground - its used to perform background tasks

onPostExecute - its used to run some finally routine after background task has done

progressCallback - it will be called every time you call publishProgress to update your UI Thread as you want

publishProgress - its used to call your progressCallback and update your UI component

setDaemon - its used to set your thread daemon

execute - called to initiate all process

interrupt - its called to interrupt your thread process

Example of usage:

public class Example extends AsyncTask {
private UIController controller;
public Example(UIController controller) {
this.controller = controller;
}
@Override
void onPreExecute() {

//This method runs on UI Thread before background task has started
this.controller.updateProgressLabel("Starting Download")
}
@Override
void doInBackground() {
//This method runs on background thread

boolean downloading = true;

while (downloading){

/*
* Your download code
*/

double progress = 65.5 //Your progress calculation
publishProgress(progress);
}
}
@Override
void onPostExecute() {
//This method runs on UI Thread after background task has done
this.controller.updateProgressLabel("Download is Done");
}
@Override
void progressCallback(Object... params) {
//This method update your UI Thread during the execution of background thread
double progress = (double)params[0]
this.controller.updateProgress(progress);
}
}
//To call this class you just need to instatiate that doing
Example testing = new Example(myController);
testing.execute();

Gradle

compile group: 'com.victorlaerte', name: 'jfx-asynctask', version: '1.0.1', ext: 'pom'

Maven

<dependency>
<groupId>com.victorlaerte</groupId>
<artifactId>jfx-asynctask</artifactId>
<version>1.0.1</version>
<type>pom</type>
</dependency>

)

Victor Oliveira

Written by

Mobile Software Engineer and Researcher at Federal University of Pernambuco

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade