Home » Core Java » Runnable vs Callable

Runnable vs Callable

 Runnable vs Callable ::

If you are a middle experienced Java Programmer and you are going to attend interview ,there is a high chance that you may face this question in the  interview room.If you are in hurry to know this answer, then read only the next paragraph and if you want to understand in details,you can go though the entire page .

Difference Runnable vs Callable ::

  1. Runnable Interface was introduced in jdk 1version, while Callable interface was introduced in jdk 1.5 version.
  2. Runnable is in java.lang  package and Callbale is in java.util.concurrent package
  3. Runnable is older than Callable.So Callable definitely provides some extra features.
  4. Runnable interface has run() method and Callable interface has call() method to achieve the task completion.
  5. run() method of Runnable does not have a return type ,   while  call() method has a generic return type Future<?>
  6. run() method does not throw checked exception ,while call() method throw checked exception.
  7. run() method involves synchronous computation and call() method involves asynchronous computation.Synchronous means complete the task in same thread  ,while asynchronous means a new separate thread is created to achieve the tasks and returns a Future value upon completion of the task.

Now you are done with answering the question to the interviewer.

To understand deep ,let us see sample programs to understand Runnable and Callable in detail.

Runnable Example :: 

interface Runnable.java

 

Program ::

output::

Callable Example :: 

interface Callable.java

Program ::

Output ::


Leave a comment

Your email address will not be published. Required fields are marked *

14 − ten =