How to use Callable in Java

In this post we will provide an example on how to use Callable in Java. Callable was introduced in Java 1.5. It is similar to the Runnable interface however, it allows you to return a value and also propagate an exception.

How to use Callable in Java

Example 1: Get callable result (blocking)

The example below shows:

  • how to run a callable task using an Executor
  • Capturing the returned value

The above future.get() call will block until the callable task is complete.

Example 2: Get callable result (with specified timeout)

The second example is similar to the first but it will not block indefinitely (specify max time to wait)

As you can see from the example above the future.get() only wait for a maximum of 2 seconds. Depending on your business logic you can decide whether to continue to wait (as above) or not.

I hope this post has given you some useful insight in how to use the callable interface.

Similar posts:

  1. Java WatchService example
  2. How to create a thread in Java
  3. Java PUT request using Unirest


  1. Runnable vs Callable in Java
  2. Java Callable API


Leave a Reply