The API for Node clearly states that one should not modify a live Node outside of the JavaFX Application Thread:
Node objects may be constructed and modified on any thread as long they are not yet attached to a Scene. An application must attach nodes to a Scene, and modify nodes that are already attached to a Scene, on the JavaFX Application Thread.
I came across this rule via a closely related forum question. Is this rule overly conservative? I've not managed to find any discussion on the matter. It seems to me there are cases where one can modify a property of a Node subclass with no repercussions. One example that seems perfectly fine is to modify the opacity value of a Group--I've been doing this, with no problems, from an AnimationTimer thread.
The project where I am doing it is described on this blog post. The jar file for the post is here.
My AnimationTime is simple:
I refer to a Group: TitleBlocks that is a live Node, in an update() method with the code line:
The above is working fine, so far.
Here's an example of a way to schedule commands on the FX Application thread:
I worry that for more involved graphics, the FX Application thread could become overwhelmed if I every instance of an opacity value change (and other changes, such as icon and sprite movements) all have to be scheduled this way.
My guess is that for certain types of properties, the code in the JavaFX Application Thread that handles the Nodes only does a read of the current value, and if the property value is changed after the fact, this is simply ignored until the next iteration. But without having this practice sanctioned (such as with the fades I employ on my game-to-be), I'm nervous about leaving my code as is.
Following is another example to consider (the one that inspired me to try writing a game with JavaFX). This particle generator application modifies 20,000 objects at the rate of 60 fps. Each object is a subclass of Node when you trace the parentage. the modifications are being done on the AnimationTimer thread, not the FX Application thread. Why doesn't this program exhibit any problems? Doesn't it violate the rule about only modifying nodes via the FX Application thread? Or am I misreading the code?
I may have found my own answer. The API for Application has the following quote:
JavaFX creates an application thread for running the application start method, processing input events, and running animation timelines. Creation of JavaFX Scene and Stage objects as well as modification of scene graph operations to live objects (those objects already attached to a scene) must be done on the JavaFX application thread.
If I understand the above correctly, the code where I alter opacities via the AnimationTimer is by definition already on the JavaFX application thread.
This would also explain why the Particle Generator code I linked also runs without any noticeable problems.
So, I think I am good. I will leave this thread "unresolved" for a while to allow others to comment/confirm or counter the solution to the question, as I am not 100% confident I have this right.
ice is for people that are not already cool. Chill with this tiny ad: