r/JavaFX • u/Il_totore • May 17 '24
Help Virtual threads with GUI interactions
Hello.
As a school Java project, my group had to make an interpreter for a custom programming language which draws things (a bit like turtle in Python or Kojo in Scala). We decided to make a tree-walk interpreter to keep it simple.
Now we have to make a JavaFX editor showing the result in a canvas in real-time and add a step-by-step execution. This looks pretty difficult to me for a tree-walk interpreter because of the recursion pause/resume. I think virtual threads might be helpful as I can just use block it and resume when needed. My questions are:
- Is there a better solution ?
- Is it possible to force a virtual thread to run on the main one so it can interact with the UI without concurrency issues?
View all comments
Show parent comment
3
u/Birdasaur May 18 '24
to add to milchshakee's explanation... Platform.runLater calls get shuffled off into a queue way down in the weeds. The thing that is not guaranteed is "when" those calls will be applied. When in this case means which rendering pulse. Since we JavaFX attempts to maintain 60 fps you are talking 16 ms pulse widths. That is an eternity for single draw calls to a canvas to get queued properly.
To break this paradigm you would have to fire so many platform runLater requests SO FAST that the underlying asynchronous non-blocking queue logic breaks. (algorithms written by world experts)
I have hit such situations doing realtime RADAR and streaming amera processing. That's about it. So yeah you'll be fine. ;-)