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 to ...

Get Java 11 Cookbook 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.