Skip to Main Content

Java SE (Java Platform, Standard Edition)

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Multi-threaded game loop in JavaFX

svanimpeJan 7 2014 — edited May 2 2014

I'm trying to understand how JavaFX's threading model works, in order to figure out the best way to write my game loops. Right now, my approach is as follows:

EventHandler<ActionEvent> gameUpdate = event ->

{

    // update the world based on a 33.3ms timestep (see below)

};

gameLoop = new Timeline(new KeyFrame(Duration.millis(33.3), gameUpdate));

gameLoop.setCycleCount(Animation.INDEFINITE);

My understanding is that:

  1. This should cause the game loop to run at approximately 30 FPS (the target frame rate), assuming that the gameUpdate doesn't take more than 33.3ms.
  2. The changes I make during the gameUpdate will cause a pulse to be triggered.
  3. This pulse is an event on the JavaFX application thread. Such a pulse ends with a synchronisation between the JavaFX application thread and a Prism render thread.
  4. The gameUpdate executes on the same application thread, so the pulse will run after the gameUpdate.
    By this I mean the gameUpdate will never be interrupted by a pulse, which could cause the world to be rendered in an in-between state.
  5. Event handlers also run on the same thread, so I need not worry about synchronizing them.
  6. Changes made to live nodes by event handlers will cause an extra pulse to be triggered.

My questions are:

  1. Am I understanding this correctly?
  2. When working with animations (eg. RotateTransition), will this animation trigger its own pulses, effectively running at a higher framerate than the game loop?

Thanks!

This post has been answered by jsmith on Jan 7 2014
Jump to Answer
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on May 30 2014
Added on Jan 7 2014
17 comments
21,120 views