<div dir="ltr">Isn't that exactly what the Worker interface and its Task and Service implementations are for? Service says<div><br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">A Service is a non-visual component encapsulating the information required to perform some work on one or more background threads. As part of the JavaFX UI library, the Service knows about the JavaFX Application thread and is designed to relieve the application developer from the burden of managing multithreaded code that interacts with the user interface.</blockquote><div><br></div><div>The example provided by Jurgen uses Task, which guarantees that the resulting Parent can be attached to the scenegraph on the FX thread (in 'succeeded()'). Up to here everything is fine. The problem starts when the code from inside Task#call, which is called on the background thread, starts an animation itself, an operation that interacts with the FX thread. Task warns about this:</div><div><br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">Because the Task is designed for use with JavaFX GUI applications, it ensures that every change to its public properties, as well as change notifications for state, errors, and for event handlers, all occur on the main JavaFX application thread. Accessing these properties from a background thread (including the call() method) will result in runtime exceptions being raised.</blockquote><div><br></div><div>and </div><div><br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">Generally, Tasks should not interact directly with the UI. Doing so creates a tight coupling between a specific Task implementation and a specific part of your UI. However, when you do want to create such a coupling, you must ensure that you use Platform.runLater so that any modifications of the scene graph occur on the FX Application Thread. </blockquote><div> </div><div>So I don't think you're missing something in your description of the problem, and I don't think anyone has argued that doing what you described is not a problem. Thankfully, I don't remember coming across code that does this. People seem to be vigilant about using javafx.concurrent tools for this purpose, which is a good sign.</div></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Thu, Jan 25, 2024 at 11:50 AM John Hendrikx <<a href="mailto:john.hendrikx@gmail.com" target="_blank">john.hendrikx@gmail.com</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">All this threading talk has made me wonder something:<br>
<br>
Let's say there are two threads, the FX thread and Thread 1.  I do the <br>
following:<br>
<br>
- On thread 1: create Label<br>
- On thread 1: set Label text to "xyz"<br>
<br>
I now attach this label to an active Scene graph. This should be done on <br>
the FX thread as we're going to be manipulating an active Scene graph, so:<br>
<br>
- On FX thread: attach Label to active scene graph<br>
<br>
There is no synchronization in place on the Label's text field. What <br>
guarantees do I have that when the label is shown on screen it will show <br>
"xyz" ?<br>
<br>
IMHO, there is no such guarantee, and so any creation or manipulation of <br>
Nodes that will later be part of an active scene graph (and thus <br>
accessed by the FX thread) **must** be done on the FX thread.  Involving <br>
any other thread in their creation or manipulation runs the risk of <br>
seeing an object in the incorrect state (or even an "impossible" state <br>
when multiple fields are involved that normally change together).<br>
<br>
Effectively, assuming that when you create Nodes you always have the <br>
intention of showing them at some point, you can never construct Nodes <br>
on any other thread than the FX thread...<br>
<br>
Am I missing something?<br>
<br>
--John<br>
<br>
</blockquote></div>