This discussion is archived
2 Replies Latest reply: Feb 12, 2013 1:27 PM by 990672 RSS

Updating the GUI from a background Thread: Platform.Runlater() Vs Tasks

990672 Newbie
Currently Being Moderated
Hi Everyone,

Hereby I would like to ask if anyone can enlighten me on the best practice for concurency with JAVAFX2. More precisely, if one has to update a Gui from a background Thread what should be the appropriate approach.

I further explain my though:

I have window with a text box in it and i receive some message on my network on the background, hence i want to update the scrolling textbox of my window with the incoming message. In that scenario what is the best appraoch.

1- Shall i implement my my message receiver as thread in which i would then use a platform.RunLater() ?

2- Or shall i use a Task ? In that case, which public property of the task shall take the message that i receive ? Are property of the task only those already defined, or any public property defined in subclass can be used to be binded in the graphical thread ?


In general i would like to understand, what is the logic behind each method ?

My understanding here, is that task property are only meant to update the gui with respect to the status of the task. However updating the Gui about information of change that have occured on the data model, requires Platform.RunLater to be used.

Edited by: 987669 on Feb 12, 2013 12:12 PM
  • 1. Re: Updating the GUI from a background Thread: Platform.Runlater() Vs Tasks
    jsmith Guru
    Currently Being Moderated
    Shall i implement my my message receiver as thread in which i would then use a platform.RunLater() ?
    Yes.
    Or shall i use a Task ?
    No.
    what is the logic behind each method?
    A general rule of thumb:
    a) If the operation is initiated by the client (e.g. fetch data from a server), use a Task for a one-off process (or a Service for a repeated process):
    - the extra facilities of a Task such as easier implementation of thread safety, work done and message properties, etc. are usually needed in this case.
    b) If the operation is initiated by the server (e.g. push data to the client), use Platform.runLater:
    - spin up a standard thread to listen for data (your network communication library will probably do this anyway) and to communicate results back to your UI.
    - likely you don't need the additional overhead and facilities of a Task in this case.

    Tasks and Platform.runLater are not mutually exclusive. For example if you want to update your GUI based on a partial result from an in-process task, then you can create the task and in the Task's call method, use a Platform.runLater to update the GUI as the task is executing. That's kind of a more advanced use-case and is documented in the Task documentation as "A Task Which Returns Partial Results" http://docs.oracle.com/javafx/2/api/javafx/concurrent/Task.html
  • 2. Re: Updating the GUI from a background Thread: Platform.Runlater() Vs Tasks
    990672 Newbie
    Currently Being Moderated
    Many thanks,


    Thinks are much more clear now. Actually my program is currently working with SWT and this is what i do, 9but new requirements came, and i have to revamp the all interface with JavaFx 2. However, when i start looking at the official tutorial on concurrency i automatically got lost. As there is no mention at all of the Platform.runLater....

    Many thx again, i got the point with the non exclusivity as well.

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points