Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

The difference between the Runnable and Callable interfaces in Java

What is the difference between using the Runnable and Callable interfaces when designing a concurrent thread in Java, why would you choose one over the other?

like image 989
Scottm Avatar asked Sep 26 '08 19:09

Scottm


People also ask

What is callable interface in Java?

Interface Callable<V> Implementors define a single method with no arguments called call. The Callable interface is similar to Runnable , in that both are designed for classes whose instances are potentially executed by another thread. A Runnable, however, does not return a result and cannot throw a checked exception.

What is the difference between runnable and running state in Java?

In the nomenclature of most operating systems, "running" means that the thread actually is executing instructions on some CPU, and "runnable" means that nothing prevents the thread from "running" except the availability of a CPU to run on. A Java program can not tell the difference between those two states.

What is runnable interface in Java?

A runnable interface in Java is an interface whose instances can run as a Thread. While working with Threads, the runnable interface acts as a core element of the Java programming language. Java classes created to run Threads must implement this interface.

What is valid about call method of callable interface in Java?

Java Callable Interface DefinitionThe call() method can return a result. If the task is executed asynchronously, the result is typically propagated back to the creator of the task via a Java Future. This is the case when a Callable is submitted to an ExecutorService for concurrent execution.


1 Answers

See explanation here.

The Callable interface is similar to Runnable, in that both are designed for classes whose instances are potentially executed by another thread. A Runnable, however, does not return a result and cannot throw a checked exception.

like image 143
Jorge Ferreira Avatar answered Oct 06 '22 01:10

Jorge Ferreira