• ... 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 52), which ...

Get Java How To Program, Late Objects, 11th Edition now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.