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()
andisDone()
by which you can get or perform various operations with respect to tasks.Ex:
12345678910111213//Runnable interface method's run method has return type as voidpublic void run(){}//Callable interface method's call method has return type as generic VV call() throws Exception;{}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 overriderun()
method, while inCallable
, we need to overridecall()
method. Runnable
is used when we don’t want any return value after completion of task for ex: logging, whileCallable
is used when we want to get a result of the computation.- First, we create the instance of class which has implemented Runnable interface and after that create instance of Thread class and then pass the object of Runnable class as parameter in Thread class.
12345Runnable Class rc=new RunnableClass();Thread t=new Thread(rc);t.start();
Was this post helpful?
Let us know if this post was helpful. Feedbacks are monitored on daily basis. Please do provide feedback as that\'s the only way to improve.