• ... another Fibonacci-Task until the current one completes, then we clear the fibonacciLabel (so an old result is not displayed when a new FibonacciTask begins).

  • Lines 44–47 use Task method setOnSucceeded to register a listener (implemented as a lambda) that’s invoked when the Task enters the succeeded state—that is, when the Task successfully runs to completion. In this case, we call the Task’s getValue method (from interface Worker) to obtain the result, which we convert to a String, then display in the fibonacciLabel. Then we enable the goButton so the user can start a new FibonacciTask.

You also can register listeners for a Task’s canceled, failed and scheduled states.

Finally, lines 50–53 use an ExecutorService to launch the FibonacciTask (line ...

Get Java How to Program, Early Objects, 11th Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.