Runnable Vs Callable in Java

Runnable: The Runnable interface should be implemented by any class whose instances are intended to be executed by a thread.The class must define a method of no arguments called run(),Runnable is available since JDK1.0

Callable:A task that returns a result and may throw an exception. Implementors define a single method with no arguments called call(),Callable is available since JDK1.5

Difference:TheCallable interface is similar to java.lang.Runnable, 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.look at the both Runnable and Callable interface

 
Runnable:

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.
 *

 * The general contract of the method <code>run</code> is that it may
 * take any action whatsoever.
 *
 */
 public abstract void run();
 }

 

Callable:

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;
}
Advertisements
This entry was posted in Interview, Java. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s