... another
Fibonacci-Task
until the current one completes, then we clear thefibonacciLabel
(so an old result is not displayed when a newFibonacciTask
begins).-
Lines 44–47 use
Task
methodsetOnSucceeded
to register a listener (implemented as a lambda) that’s invoked when theTask
enters the succeeded state—that is, when theTask
successfully runs to completion. In this case, we call theTask
’sgetValue
method (from interfaceWorker
) to obtain the result, which we convert to aString
, then display in thefibonacciLabel
. Then we enable thegoButton
so the user can start a newFibonacciTask
.
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.