How it works...

Let's move a step closer to real-life code and create a class that implements Callable and allows you to return a result from a worker as an object of the Result class:    

class Result {  private int sleepSec, result;  private String workerName;  public Result(String workerName, int sleptSec, int result) {    this.workerName = workerName;    this.sleepSec = sleptSec;    this.result = result;  }  public String getWorkerName() { return this.workerName; }  public int getSleepSec() { return this.sleepSec; }  public int getResult() { return this.result; }}

An actual numeric result is returned by the getResult() method. Here, we also included the name of the worker and how long the thread is expected to sleep (to work) just for convenience and ...

Get Java 11 Cookbook now with O’Reilly online learning.

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