Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.4k views
in Technique[技术] by (71.8m points)

multithreading - How JavaFX application thread works?

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();

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.

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

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

Can somebody help me to fix this??

Thank you in advance!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.

Execute all your code in the application main thread, and perform only GUI tasks in JavaFX thread, by calling Platform.runLater.

References on this topic:


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...