This week's giveaway is in the Testing forum.
We're giving away four copies of TDD for a Shopping Website LiveProject and have Steven Solomon on-line!
See this thread for details.
Win a copy of TDD for a Shopping Website LiveProject this week in the Testing forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
  • Piet Souris
  • Himai Minh

When (if ever) is it okay to modify a Node property outside of the JavaFX Application thread?

Ranch Hand
Posts: 140
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?

Blogpost (describes code, has video and link to code):

Code (so you can jump to it directly, if desired):
Phil Freihofner
Ranch Hand
Posts: 140
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
A lot of people cry when they cut onions. The trick is not to form an emotional bond. This tiny ad told me:
free, earth-friendly heat - a kickstarter for putting coin in your pocket while saving the earth
    Bookmark Topic Watch Topic
  • New Topic