Usage of JavaFX Platform.runLater and access to UI

2019-01-09 04:17发布

I have a few questions about Platform.runLater. I have a JavaFX Application class. In this class I run a thread (The thread reads data from a network socket).

Now when I create a new Stage inside the thread, the system throws an execption (JavaFX event dispatcher thread and my netork-read thread are not the same) - I understand this behaviour.

But the other side is, I append the text from the network-reader to an existing TextArea or add/remove some items in a ListView<String> - this doesn't throw an exception - why ? I thought JavaFX is singlethreaded (The ui library part). Is this the same thing as in Swing: Sometimes it works and sometimes you have just garbage (Because EDT)?

My questions:

  • When does the JavaFX event dispatcher thread throw an exception and when not ?
  • Are the any good documents about this
  • Is there an easier (shorter & cleaner) way to use Platform.runLater with a run() method ? In combination with a try catch (or multiple catch), it looks very strange

I know the usage of Platform.runLater in a thread isn't that nice (design solution)

2条回答
成全新的幸福
2楼-- · 2019-01-09 05:01

"this doesn't throw an exception - why?" because not all such cases are cought... Possibly, because of performance considerations, possibly, it is just a missed functionality.

All interactions with JavaFX objects (including creation) must be done on JFX thread, if you want to access those JFX objects from another thread - use runLater or runAndWait methods. If it doesn't throw exception now, it possibly will start to throw exceptions in the future. Any interaction with JFX object may cause a subsequent actions and event, which will be cought by some thread checker - you cannot be sure.

I don't think, there is any good documentation on this - just a simple rule - use runLater or runAndWait.

Shorter and cleaner way - will be provided in JDK 8 using Lambda.

查看更多
兄弟一词,经得起流年.
3楼-- · 2019-01-09 05:18

Alexander's answer catches the most important points regarding your questions.

This answer provides some supplemental information.

When does the JavaFX event dispatcher thread throw an exception and when not ?

The JavaFX system does not always check that accesses to objects affecting the active scene graph are properly restricted to the JavaFX thread. Ultimately, ensuring such thread safety is the responsibility of the JavaFX application programmer - not the JavaFX system. You must be very careful when performing multi-threaded programming in JavaFX, otherwise application behaviour may fail or become unpredictable.

Are the any good documents about this

Try the the JavaFX tutorial: Concurrency in JavaFX.

Is there an easier (shorter & cleaner) way to use Platform.runLater with a run() method?

No. Platform.runLater is as simple as it gets.


As an aside . . .

Task and Service

Consider using the Task or Service subclasses of Worker. These are JavaFX wrappers for FutureTask (which is in turn a Runnable). Workers provide a call method to run logic on a background thread. They maintain execution status (with thread safe callback notification to the JavaFX thread for state changes) and return results of the call via value, message and exception properties.

Take advantage of the design patterns in the Task and Service javadoc examples to simplify creation of thread-safe applications with features such as:

  • Asynchronous fetching of data for UI update.
  • Periodic message updates for task progress.
  • Constructing Node graphs which are not yet attached to a displayed scene.
  • Monitoring progress via progress bars, etc.

A Worker is complementary to Platform.runLater. Use Platform.runLater when you are executing off of the JavaFX Application Thread and you want to run some logic on the JavaFX application Thread. Use a Worker when you are running on the JavaFX Application Thread and want to spawn some logic or (especially) I/O on a new thread so that you don't block the JavaFX Application Thread. You would never want to do network I/O inside a Platform.runLater's run method, but would often want to do it in a Worker's call method.

Also, usage of Task and Service is not incompatible with use of Platform.runLater. For instance, if you have a very long running Task from which you want to return partial results to the UI periodically or as a buffer fills, then executing Platform.runLater in the task's call method is the way to do that.

Workers are useful when you don't have an existing threaded service provided by a library, but are instead creating your own threads to execute in the background. If you have an existing threaded service, then you will need to use Platform.runLater to perform logic on the JavaFX application thread.

Note that you still need to know what you are doing, even if you use a Worker. You must still take care that you don't violate standard JavaFX concurrency rules, such as never updating Nodes on the active scene graph (including not updating values that Nodes in the active scene graph are bound to - such as the observable list of items backing a ListView).

查看更多
登录 后发表回答