Fork me on GitHub

Thread Management

Writing concurrent application could be painful ! Not with JRebirth

Thread Management

Threading overview

JRebirth is multi-threaded, not only by using Task Worker provided by JavaFX APIs. JRebirth has got its own Thread to manage local events (called waves).

Thus all inner JRebirth tasks are processed into a custom thread and don't infer with the JavaFX Application Thread which have to manage user events and some UI instantiation.

When you debug a JRebirth application, you can observe a lot of threads, Don't be scared !!

The most important are :

  • JavaFX Application Thread - JAT
  • JRebirth Engine Thread - JIT
  • JRebirth Slot x (n threads available in JRebirth Thread Pool - JTP)

Other threads are related to JavaFX platform, JRebirth only creates 2-5 threads that can be extended if required.

UML Class Diagram:

Thread Rules

All UI tasks must be done into the JAT, but you can build a view into the JIT or JTP but you must add it to your scene into the JAT.

All Wave processing tasks are automatically done into the JIT.

When the JRebirth Framework needs to update UI, it's done automatically into the JAT. No matters to have !

But when you call a component directly( getCommand, getService, getModel), your call is processed into the current thread.

Pay attention to the thread into you are before calling synchronous methods getCommand, get getModel, getService.

If you are into the JIT:

  • getCommand => the command will be processed synchronously into the JIT
  • getModel => all called methods of the model will be processed into the JIT ==> Can Break !!!

When you are into the JIT you must use Platform.runLater to perform UI updates. JRebirth Framework offers some default commands to do the trick ( DefaultUiCommand ). Moreovere if you send a Wave with a WaveType listend by a model, it will be automatically processed into the JAT.

If you are into the JAT:

  • getCommand => future task will be done into the JAT : Don't perform long task or you will freeze the application !!!
  • getModel => actions will be done into the JAT ==> OK for short Ui actions

When you are in the JAT you must use JRebirthThread.runLater to run tasks into the core thread. JRebirth Framework offers some default commands to force to run into the JIT ( DefaultCommand or DefaultPoolCOmmand ). You can also send a wave that will trigger a command, or call a service into another thread that JAT.

For example:

If you want to call a service which make an asynchronuous call to a remote server. You can use the getService method to initiate the call (By default this call will be managed into JRebirth Thread Pool : JTP), but the return must be managed into the JAT:

  • Either use a wave to send data received from remote server, default service will generate a such wave (automatically processed into the JAT for models taht listen it)
  • Either call the model from the JAT and access to service results (the first way way is the best to use)