JavaFX 应用程序线程如何工作? [英] How JavaFX application thread works?

查看:40
本文介绍了JavaFX 应用程序线程如何工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了 Java FX 应用程序线程的问题.这是一个伪代码:

I have a problem with Java FX application thread. Here is a pseudo-code:

showMenu();
//Contoller which waits for action(pressing button)...
showLoadingPic();
Thread.sleep(2000);
showMap();

问题是sleep发生在showMenu()中显示的窗口中,showLoadingPic()根本没有显示,在showMap()中的最后窗口显示.

The problem is that the sleep occurs in window which is displayed in showMenu(), showLoadingPic() is not shown at all, and at the end window in showMap() is shown.

showLoadingPic 中的场景有一个进度条,它运行 2 秒,与 Thread.sleep(2000) 的时间相同.

The scene in showLoadingPic has a progress bar which runs 2 secs which is the same time as Thread.sleep(2000).

所以看起来 javafx 应用程序线程阻止 showLoadingPic() 和 showLoadingPic() 在后台运行.

So it seems like javafx application thread blocks showLoadingPic() and showLoadingPic() runs at background.

有人可以帮我解决这个问题吗??

Can somebody help me to fix this??

先谢谢你!

推荐答案

有一个 Java FX 事件调度线程,它处理所有与 GUI 相关的任务.您必须更新此线程中的所有 UI 组件.长时间运行的任务,如 Thread.sleep 不应该在这个线程中执行,因为窗口会挂起,GUI 会冻结.

There is a Java FX event dispatch thread, which handle all GUI-related tasks. You must update all UI components in this thread. Long-running tasks, like Thread.sleep should never be executed in this thread, since windows will hang, and the GUI will be frozen.

通过调用Platform.runLater.

关于这个主题的参考:

  • Concurrency in JavaFX, from Oracle
  • Related SO question

这篇关于JavaFX 应用程序线程如何工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆