Thought experiment: constructing Nodes on a different thread...

John Hendrikx john.hendrikx at gmail.com
Thu Jan 25 09:50:35 UTC 2024


All this threading talk has made me wonder something:

Let's say there are two threads, the FX thread and Thread 1.  I do the 
following:

- On thread 1: create Label
- On thread 1: set Label text to "xyz"

I now attach this label to an active Scene graph. This should be done on 
the FX thread as we're going to be manipulating an active Scene graph, so:

- On FX thread: attach Label to active scene graph

There is no synchronization in place on the Label's text field. What 
guarantees do I have that when the label is shown on screen it will show 
"xyz" ?

IMHO, there is no such guarantee, and so any creation or manipulation of 
Nodes that will later be part of an active scene graph (and thus 
accessed by the FX thread) **must** be done on the FX thread.  Involving 
any other thread in their creation or manipulation runs the risk of 
seeing an object in the incorrect state (or even an "impossible" state 
when multiple fields are involved that normally change together).

Effectively, assuming that when you create Nodes you always have the 
intention of showing them at some point, you can never construct Nodes 
on any other thread than the FX thread...

Am I missing something?

--John



More information about the openjfx-dev mailing list