JavaFX Concurrency

104 篇文章 5 订阅

The term JavaFX concurrency refers to how JavaFX is designed with respect to multithreading and concurrency. In this JavaFX concurrency tutorial I will explain the JavaFX threading model.

If you are new to Java concurrency and multithreading, I have a longer Java Concurrency tutorial explaining how it works in richer detail than what there is space for in this text.

Single-threaded Rendering

JavaFX uses a single-threaded rendering design, meaning only a single thread can render anything on the screen, and that is the JavaFX application thread. In fact, only the JavaFX application thread is allowed to make any changes to the JavaFX Scene Graph in general.

A single-threaded rendering design is easier to implement correctly, but long-running tasks that run within the JavaFX application thread make the GUI unresponsive until the task is completed. No JavaFX GUI controls react to mouse clicks, mouse over, keyboard input while the JavaFX application thread is busy running that task.

Platform.runLater()

Sometimes you absolutely need to perform some long-running task in a JavaFX application. You don't want to leave the GUI unresponsive while the task is running, so you want to run the ask in its own thread. However, you would like the running task to update the GUI - either along the way, or when the task is completed. The task thread cannot update the GUI (the scene graph) directly - but JavaFX has a solution for this problem.

JavaFX contains the Platform class which has a runLater() method. The runLater() method takes a Runnable which is executed by the JavaFX application thread when it has time. From inside this Runnable you can modify the JavaFX scene graph. Here is an example showing a task thread updating a JavaFX ProgressBar while it is executing - by calling Platform.runLater() and update the ProgressBar control from in there:

import javafx.application.Application;
import javafx.application.Platform;
import javafx.scene.Scene;
import javafx.scene.control.ProgressBar;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

public class ConcurrencyExample extends Application {
  public static void main(String[] args) {
    launch(args);
  }

  @Override
  public void start(Stage primaryStage) {
    primaryStage.setTitle("JavaFX App");

    ProgressBar progressBar = new ProgressBar(0);

    VBox vBox = new VBox(progressBar);
    Scene scene = new Scene(vBox, 960, 600);

    primaryStage.setScene(scene);
    primaryStage.show();

    Thread taskThread = new Thread(new Runnable() {
      @Override
      public void run() {
        double progress = 0;
        for(int i=0; i<10; i++){

          try {
            Thread.sleep(1000);
          } catch (InterruptedException e) {
            e.printStackTrace();
          }

          progress += 0.1;
          final double reportedProgress = progress;

          Platform.runLater(new Runnable() {
            @Override
            public void run() {
              progressBar
                  .setProgress(reportedProgress);
            }
          });
        }
      }
    });

    taskThread.start();
  }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值