In this tutorial, we will explore JavaFX Concurrency and learn how to make use of JavaFX Background Tasks to make the JavaFX applications run faster, smoother, and snappier.
JavaFX main thread (or the GUI thread) is responsible for drawing and refreshing the contents shown on the screen 60 times a second. That means the delay between each frame is only 16.67 milliseconds. If we do complex calculations or time-consuming blocking works on this main thread, then it will not be able to reach the 60fps target. In fact, it can even reduce to 1 frame per 3s or 5s which could make the application "not responding". If the application is non-responsive for a couple of seconds, the OS will start showing the "not responding" title on the title.
JavaFX provides support for offloading time-consuming tasks into a background thread using the Task approach. Tasks provide a very easy and direct approach for executing code asynchronously while providing constant updates to the GUI thread. In this chapter, we will explore the javafx.concurrent.Task and see how to make use of it to seamlessly run complex background operations in a separate thread while updating the GUI consistently.
Github Project: https://github.com/afsalashyana/JavaF...
Genuine Coder Blog: https://www.genuinecoder.com/
#JavaFX #Multithreading #Java
Introduction: (0:00)
Understand the problem with the GUI thread: (2:33)
Create a new background task: (04:29)
Update label from background task: (09:52)
Add progress bar to track progress: (12:57)
How to cancel a running task: (17:14)