O'Reilly logo

Java 9 Cookbook by Nick Samoylov, Mohamed Sanaulla

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required