Tuesday, May 20, 2014

Ruannable vs Callable



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

RunnableCallable>
Introduced in Java 1.0Introduced in Java 1.5 as part of java.util.concurrent library
Runnable cannot be parametrized Callable is a parametrized type whose type parameter indicates the return type of its run method
Classes implementing Runnable needs to implement run() methodClasses implementing Callable needs to implement call() method
Runnable.run() returns no ValueCallable.call() returns a value of Type T
Can not throw Checked ExceptionsCan throw Checked Exceptions
public class RunnableTest implements Runnable {
              @Override
              public void run() {
                         //any processing
              }
}
import java.util.concurrent.Callable;


public class CallableTest implements Callable { 
          @Override 
           public String call() throws Exception { 
                      // any processing
                    return new String("I am Callable and can return value and throw checked exception"); 
              } 
}

No comments:

Post a Comment