Difference between Runnable and Callable in java

Previous
Next

Runnable and Callable interface both are used in the multithreading environment.Callable is available in java.util.concurrent.Callable package and Runnable in java.lang.Thread.

Difference between Runnable and Callable interface in java

  • Runnable was introduced in java 1.0 version While Callable is an extended version of Runnable and introduced in java 1.5 to address the limitation of Runnable.
  • Runnable does not return any value, its return type is void While Callable have a return type.So, after completion of task, we can get the result using get() method of Future class.Future class has various methods such as get, cancel and isDone by which you can get or perform various operations with respect to tasks.Ex:

    Callable is a generic interface means implementing class will decide the type of value it will return.
  • Runnable does not throws checked exception while Callable throws Checked exception(i.e exception that are checked at compile time)so, at compile time we can identify the error.
  • In Runnable we override run() method while in Callable we need to override call() method.
  • Runnable is used when we don’t want any return value after completion of task for ex: logging While Callable is used  when we want to get a result of the computation.
  • First we create the instance of class which has implements Runnable interface and after that create instance of Thread class and than pass the object of Runnable class as parameter in Thread class.

    In case of Callable,we can not pass Callable into Thread to execute,so we need to use ExecutorService to execute Callable object.

Latest posts by Arpit Porwal (see all)

Previous
Next

Join Our News Letter – Stay Updated

Subscribe to Awesome Java Content.




Add Comment

Join Our News Letter - Stay Updated

Subscribe to Awesome Java Content.
You can like our facebook page Java2blog