callable()
is a static method of the Executors
class that returns a Callable
. When called, it runs the passed Runnable task
and returns null
.
Read What is the difference between runnable and callable in Java? to understand the difference between
Runnable
andCallable
.
public static Callable<Object> callable(Runnable task)
Runnable task
: The runnable task to run.This method returns a Callable
object.
import java.util.concurrent.Callable;import java.util.concurrent.ExecutionException;import java.util.concurrent.ExecutorService;import java.util.concurrent.Executors;public class Main {public static void main(String[] args) throws ExecutionException, InterruptedException {ExecutorService executorService = Executors.newSingleThreadExecutor();Runnable runnable = () -> System.out.println("Runnable executed");Callable<Object> callable = Executors.callable(runnable);System.out.println("Return value - " + executorService.submit(callable).get());executorService.shutdown();}}
callable()
method, we get a Callable
object passing the runnable defined in line 10 as the argument.Callable
to the ExecutorService
and print the result to the console. The result is null
.ExecutorService
.