Tuesday, 26 August 2014

Difference between Callable and Runnable in java

The first thing both Callable and Runnable are interface. Runnable might be heard a lot because it is used to create Thread. But Callable is added on java5 where as Runnable is present from jdk1.0.

Let's try to understand both -
Runnable- It is a interface and present from jdk1.0.It has run() method where we can execute task. method signature is given below-

public void run()
{
//task
}
here method return type is void
 
When we execute sometask in different thread then we can use Runnable interface but the thread can not return any result in this case because the return type is void.  And this execute by Thread. e.g.

class MyRunnable implements Runnable{
 public void run(){
  System.out.print("runabletask")
 }
 
public static void main (String args[]){

  Thread t= new Thread(new MyRunnable());
  t.start();
 
  //or start runnable by executor
  //ExecutorService executor =  Executors.newFixedThreadPool(2);
  //executor.execute(new MyRunnable());
 }
} 

Callable - It is also a interface which is present from jdk5.0.It has call() method. It is used for parallel execution hence it is part of concurrent api, present in java.util.concurrent package.
Method signature is given below

public V call()throws Exception
{
//task
}
 
here method return type is Generic type V That means, can be passed from parameter of class  This return called future object of java.util.concurrent.Future package. As run method doesn't return any result so, in jdk5.0 introduced Callable.In some situation when we want return some result after thread execution finish then we can use Callable interface.This also execute by Thread. e.g.

class MyCallable implements Callable<String>{ 
 
 public String call()throws Exception{
  System.out.print("callable task")
 }

public static void main(String...args){ 
 ExecutorService executor =  Executors.newFixedThreadPool(2);
 
 List<Future<String>> list = new ArrayList<Future<String>>();
 
 MyCallable  callable = new MyCallable();
 for(int i=0; i< 100; i++){
  Future <String>future = executor.submit(callable);
  list.add(future);
 }
 for(Future <String>future:list){
  try {
    System.out.println(future.get());
  } catch (Exception e) {
    e.printStackTrace();
  }
 }
  executer.shutdown(); 
 } 

To create Thread pool in java Executer class used and These Thread pool will 
hold Thread to execute Callable or Runnable in a thread pool.Since callable or Runnable tasks run in parallel, we have to wait for the returned 
Object in case of Callable. Callable tasks return java.util.concurrent.Future object. 
Using Future we can find out the status of the Callable task and get the 
returned Object. It provides get() method that can wait for the Callable to 
finish and then return the result.

Future also provides cancel() method to cancel the Callable task. There is an overloaded version of get() method
 where we can specify the time to wait for the result, it’s useful to 
avoid current thread getting blocked for longer time. There are isDone() and isCancelled() methods to find out the current status of associated Callable task.



 

No comments:

Post a Comment