Rectangle 27 0

multithreading The difference between the Runnable and Callable interfaces in Java?


Callable

(@prash - Also ... by starting to use Java in the Java 1.1 era.)

@StephenC If I read your answer correctly, you're suggesting that Runnable exists (largely) for backward compatibility reasons. But aren't there situations where it's unnecessary or too expensive to implement (or to require) Callable interface (e.g., in ScheduledFuture<?> ScheduledExecutorService.schedule(Runnable command, long delay, TimeUnit unit)) ? So isn't there a benefit to maintaining both interfaces in the language even the history didn't force the current outcome?

@max - Well I said that, and I still agree with that. However, that is a secondary reason. But even so, I suspect that Runnable would have been modified if there had not been an imperative to maintain compatibility. The "boilerplate" of return null; is a weak argument. (At least, that would have been my decision ... in the hypothetical context where you could ignore backwards compatibility.)

@prash - the basic facts are to be found in old textbooks. Like the first edition of Java in a Nutshell.

And, obviously, there are use-cases where a task doesn't need to return a result or throw a checked exception. For those use-cases, using Runnable is more concise than using Callable<Void> and returning a dummy (null) value from the call() method.

Because the Runnable interface cannot do everything that Callable does!

Runnable has been around since Java 1.0, but Callable was only introduced in Java 1.5 ... to handle use-cases that Runnable does not support. In theory, the Java team could have changed the signature of the Runnable.run() method, but this would have broken binary compatiblity with pre-1.5 code, requiring recoding when migrating old Java code to newer JVMs. That is a BIG NO-NO. Java strives to be backwards compatible ... and that's been one of Java's biggest selling points for business computing.

What are the differences in the applications of Runnable and Callable. Is the difference only with the return parameter present in Callable?

What is the need of having both if Callable can do all that Runnable does?

Note
Rectangle 27 0

multithreading The difference between the Runnable and Callable interfaces in Java?


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.

Note
Rectangle 27 0

multithreading The difference between the Runnable and Callable interfaces in Java?


As it was already mentioned here Callable is relatively new interface and it was introduced as a part of concurrency package. Both Callable and Runnable can be used with executors. Class Thread (that implements Runnable itself) supports Runnable only.

Code running in custom thread as any other code can throw exception. To catch it in other thread you have to perform some efforts either using custom notification mechanism (e.g. based on listeners) or by using Future or by adding hook that catches all uncought exceptions: docs.oracle.com/javase/6/docs/api/java/lang/

I upvoted this answer because it asserts (correctly if taken at face value) one must use the thread pool model with callable objects. The apparently unfortunate thing about this is that one cannot extend Thread to make meaningful use of the Callable interface so that a single thread can be customised to do callable things and other things the developer might want. If anyone who reads this comment thinks I am wrong, I'd like to know better...

You can still use Runnable with executors. The advantage of Callable that you can send it to executor and immediately get back Future result that will be updated when the execution is finished. The same may be implemented with Runnable, but in this case you have to manage the results yourself. For example you can create results queue that will hold all results. Other thread can wait on this queue and deal with results that arrive.

i wonder what is the example on a thread throwing exception out in java? will the main thread be able to catch that exception? If not, i wouldn't use Callable. Alex, do you have some insight on this? thanks!

Note
Rectangle 27 0

multithreading The difference between the Runnable and Callable interfaces in Java?


Sign up for our newsletter and get our top new questions delivered to your inbox (see an example).

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.

Note
Rectangle 27 0

multithreading The difference between the Runnable and Callable interfaces in Java?


Callable

(@prash - Also ... by starting to use Java in the Java 1.1 era.)

@StephenC If I read your answer correctly, you're suggesting that Runnable exists (largely) for backward compatibility reasons. But aren't there situations where it's unnecessary or too expensive to implement (or to require) Callable interface (e.g., in ScheduledFuture<?> ScheduledExecutorService.schedule(Runnable command, long delay, TimeUnit unit)) ? So isn't there a benefit to maintaining both interfaces in the language even the history didn't force the current outcome?

@max - Well I said that, and I still agree with that. However, that is a secondary reason. But even so, I suspect that Runnable would have been modified if there had not been an imperative to maintain compatibility. The "boilerplate" of return null; is a weak argument. (At least, that would have been my decision ... in the hypothetical context where you could ignore backwards compatibility.)

@prash - the basic facts are to be found in old textbooks. Like the first edition of Java in a Nutshell.

And, obviously, there are use-cases where a task doesn't need to return a result or throw a checked exception. For those use-cases, using Runnable is more concise than using Callable<Void> and returning a dummy (null) value from the call() method.

Because the Runnable interface cannot do everything that Callable does!

Runnable has been around since Java 1.0, but Callable was only introduced in Java 1.5 ... to handle use-cases that Runnable does not support. In theory, the Java team could have changed the signature of the Runnable.run() method, but this would have broken binary compatiblity with pre-1.5 code, requiring recoding when migrating old Java code to newer JVMs. That is a BIG NO-NO. Java strives to be backwards compatible ... and that's been one of Java's biggest selling points for business computing.

What are the differences in the applications of Runnable and Callable. Is the difference only with the return parameter present in Callable?

What is the need of having both if Callable can do all that Runnable does?

Note
Rectangle 27 0

multithreading The difference between the Runnable and Callable interfaces in Java?


<T> Future<T> submit(Callable<T> task);
Future<?> submit(Runnable task);
<T> Future<T> submit(Runnable task, T result);
Callable
Runnable
public interface Callable<V> {
    /**
     * Computes a result, or throws an exception if unable to do so.
     *
     * @return computed result
     * @throws Exception if unable to compute a result
     */
    V call() throws Exception;
}
public interface Runnable {
    /**
     * When an object implementing interface <code>Runnable</code> is used 
     * to create a thread, starting the thread causes the object's 
     * <code>run</code> method to be called in that separately executing 
     * thread. 
     * <p>
     * The general contract of the method <code>run</code> is that it may 
     * take any action whatsoever.
     *
     * @see     java.lang.Thread#run()
     */
    public abstract void run();
}
  • A Runnable object cannot throw a checked exception wheras a Callable object can throw an exception.
  • A Runnable object does not return a result whereas a Callable object returns a result.
  • Both are functional interfaces and can be used in Lambda expressions since Java8.
  • Instance of both Callable and Runnable interfaces can be executed by ExecutorService via submit() method.
  • Instances of the classes that implement Runnable or Callable interfaces are potentially executed by another thread.

Callable interface declares call() method and you need to provide generics as type of Object call() should return -

Methods in ExecutorService interface are

Runnable on the other hand is interface that declares run() method that is called when you create a Thread with the runnable and call start() on it. You can also directly call run() but that just executes the run() method is same thread.

To summarize few notable Difference are

Note
Rectangle 27 0

multithreading The difference between the Runnable and Callable interfaces in Java?


<T> Future<T> submit(Callable<T> task);
Future<?> submit(Runnable task);
<T> Future<T> submit(Runnable task, T result);
Callable
Runnable
public interface Callable<V> {
    /**
     * Computes a result, or throws an exception if unable to do so.
     *
     * @return computed result
     * @throws Exception if unable to compute a result
     */
    V call() throws Exception;
}
public interface Runnable {
    /**
     * When an object implementing interface <code>Runnable</code> is used 
     * to create a thread, starting the thread causes the object's 
     * <code>run</code> method to be called in that separately executing 
     * thread. 
     * <p>
     * The general contract of the method <code>run</code> is that it may 
     * take any action whatsoever.
     *
     * @see     java.lang.Thread#run()
     */
    public abstract void run();
}
  • A Runnable object cannot throw a checked exception wheras a Callable object can throw an exception.
  • A Runnable object does not return a result whereas a Callable object returns a result.
  • Both are functional interfaces and can be used in Lambda expressions since Java8.
  • Instance of both Callable and Runnable interfaces can be executed by ExecutorService via submit() method.
  • Instances of the classes that implement Runnable or Callable interfaces are potentially executed by another thread.

Callable interface declares call() method and you need to provide generics as type of Object call() should return -

Methods in ExecutorService interface are

Runnable on the other hand is interface that declares run() method that is called when you create a Thread with the runnable and call start() on it. You can also directly call run() but that just executes the run() method is same thread.

To summarize few notable Difference are

Note
Rectangle 27 0

multithreading The difference between the Runnable and Callable interfaces in Java?


  • A Callable<V> instance may throw checked exceptions, whereas a Runnable instance can't
  • A Callable<V> instance returns a result of type V, whereas a Runnable instance doesn't.

I found this in another blog that can explain it a little bit more these differences:

The designers of Java felt a need of extending the capabilities of the Runnable interface, but they didn't want to affect the uses of the Runnable interface and probably that was the reason why they went for having a separate interface named Callable in Java 1.5 than changing the already existing Runnable.

Though both the interfaces are implemented by the classes who wish to execute in a different thread of execution, but there are few differences between the two interface which are:

Note
Rectangle 27 0

multithreading The difference between the Runnable and Callable interfaces in Java?


Callable : If you are trying to retrieve a value from a task, then use Callable. Now callable on its own will not do the job. You will need a Future that you wrap around your Callable and get your values on future.get (). Here the calling thread will be blocked till the Future comes back with results which in turn is waiting for Callable's call() method to execute.

NOTE : Inside your target class you can make the calls to Callable and Runnable on a single thread executor, making this mechanism similar to a serial dispatch queue. So as long as the caller calls your Runnable wrapped methods the calling thread will execute really fast without blocking. As soon as it calls a Callable wrapped in Future method it will have to block till all the other queued items are executed. Only then the method will return with values. This is a synchronization mechanism.

Runnable : If you have a fire and forget task then use Runnable. Put your code inside a Runnable and when the run() method is called, you can perform your task. The calling thread really does not care when you perform your task.

Runnable and Callable both run on a different thread than the calling thread. But Callable can return a value and Runnable cannot. So where does this really apply.

So think about an interface to a target class where you have both Runnable and Callable wrapped methods defined. The calling class will randomly call your interface methods not knowing which is Runnable and which is Callable. The Runnable methods will execute asynchronously, till a Callable method is called. Here the calling class's thread will block since you are retrieving values from your target class.

Note
Rectangle 27 0

multithreading The difference between the Runnable and Callable interfaces in Java?


//System.out.println("future: call ="+future.get());
Future
System.out.println("future: call ="+future.get());
get()
import java.util.concurrent.*;
import java.util.*;

public class CallableDemo{
    public CallableDemo(){
        System.out.println("creating service");
        ExecutorService service = Executors.newFixedThreadPool(10);

        List<MyCallable> futureList = new ArrayList<MyCallable>();
        for ( int i=0; i<12; i++){
            MyCallable myCallable = new MyCallable((long)i);
            futureList.add(myCallable);
        }
        System.out.println("Start");
        try{
            List<Future<Long>> futures = service.invokeAll(futureList);  
            for(Future<Long> future : futures){
                try{
                    System.out.println("future.isDone = " + future.isDone());
                    //System.out.println("future: call ="+future.get());
                }
                catch(Exception err1){
                    err1.printStackTrace();
                }
            }
        }catch(Exception err){
            err.printStackTrace();
        }
        service.shutdown();
    }
    public static void main(String args[]){
        CallableDemo demo = new CallableDemo();
    }
    class MyCallable implements Callable<Long>{
        Long id = 0L;
        public MyCallable(Long val){
            this.id = val;
        }
        public Long call(){
            int a=4, b = 0;
            System.out.println("a/b:"+(a/b));
            return id;
        }
    }
}
java CallableDemo
creating service
Start
future.isDone = true
java.util.concurrent.ExecutionException: java.lang.ArithmeticException: / by zero
        at java.util.concurrent.FutureTask.report(FutureTask.java:122)
        at java.util.concurrent.FutureTask.get(FutureTask.java:188)
        at CallableDemo.<init>(CallableDemo.java:19)
        at CallableDemo.main(CallableDemo.java:27)
Caused by: java.lang.ArithmeticException: / by zero
        at CallableDemo$MyCallable.call(CallableDemo.java:36)
        at CallableDemo$MyCallable.call(CallableDemo.java:29)
        at java.util.concurrent.FutureTask.run(FutureTask.java:262)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
        at java.lang.Thread.run(Thread.java:744)

If you submit Runnable by using Executor.html#execute, it will return Exceptions during the processing of Runnable task.

If you submit the Callable and did not check the status of Future task (by calling get() method on Future), you will be misled with result of future task.

Now uncomment the code from

The differences between Callable and Runnable & use cases to use each of them are clear from earlier answers.

The other ways of handling these type of exceptions are explained in :

These types of Exceptions are caught by Framework internally and hidden.

Note