Showing posts with label SGM. Show all posts
Showing posts with label SGM. Show all posts

Monday, 15 May 2017

Callable example

java.util.concurrent.Callable has been introduced in JDK 5. Callable is a thread that returns the result.

Callable.call() method
We need to implement(override) the call() of Callable interface for task computation and submit() method of ExecutorService can be used to run Callable.
To fetch the result of call() method of the Callable interface, ExecutorService.submit() method returns a Future instance and using the get() method of Future, we can fetch the result of call() method of Callable.

ExecutorService also provides invokeAll() and invokeAny() method to run Callable threads.

Java Future
As Java Callable tasks return java.util.concurrent.Future object. Using Java Future object, 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.
Java Future provides cancel() method to cancel the associated 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 the longer time. There are isDone() and isCancelled() methods to find out the current status of the associated Callable task.

Callable Example in Java
package com.thread;

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;


class CallableTask implements Callable<Integer> {
    
     private int num = 0;
    
     public CallableTask(int num){
           this.num = num;
     }
    
     @Override
     public Integer call() throws Exception {
           int result = 1;
           for(int i=1;i<=num;i++){
                result*=i;
           }
           return result;
     }
}

/**
 * Calculate the Factorial using callable.
 * @author rajesh dixit
 */
public class CallableDemo {
    
     public static void main(String[] args) throws InterruptedException, ExecutionException {
          
           ExecutorService service =  Executors.newSingleThreadExecutor();
          
           CallableTask sumTask = new CallableTask(10);
          
           Future<Integer> future = service.submit(sumTask);
          
           Integer result = future.get();
           System.out.println(result);
     }
}

Difference between Callable and Runnable

The Callable interface is introduced in Java 5, however, Runnable interface is from JDK 1.0. Callable can return the result of an operation performed inside call() method, which was one of the limitations with the Runnable interface.

Another significant difference between Runnable and Callable interface is the ability to throw checked exception. The Callable interface can throw checked exception because its call method throws Exception.

Commonly FutureTask is used along with Callable to get the result of asynchronous computation task performed in call() method.

Callable vs Runnable interface
1) Runnable interface has run() method to define task while Callable interface uses call() method for task definition.

2) run() method does not return any value, it's return type is void while call method returns value. The Callable interface is a generic parameterized interface and Type of value is provided when an instance of Callable implementation is created.

3) Another difference on run and call method is that run method cannot throw checked exception while call method can throw checked exception.

Callable Example in Java
package com.thread;

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;


class CallableTask implements Callable<Integer> {
    
     private int num = 0;
    
     public CallableTask(int num){
           this.num = num;
     }
    
     @Override
     public Integer call() throws Exception {
           int result = 1;
           for(int i=1;i<=num;i++){
                result*=i;
           }
           return result;
     }
}

/**
 * Calculate the Factorial using callable.
 * @author rajesh dixit
 */
public class CallableDemo {
    
     public static void main(String[] args) throws InterruptedException, ExecutionException {
          
           ExecutorService service =  Executors.newSingleThreadExecutor();
          
           CallableTask sumTask = new CallableTask(10);
          
           Future<Integer> future = service.submit(sumTask);
          
           Integer result = future.get();

           System.out.println("Factorial of 10 is "+result);
     }
}



Future and FutureTask in Java

Future and FutureTask allows us to write asynchronous code. Future is a general concurrency abstraction, also known as a promise, which promises to return a result in future.

In asynchronous programming, main thread doesn't wait for any task to finished, rather it hands over the task to workers and move on. One way of asynchronous processing is using callback methods. Future is another way to write asynchronous code. By using Future and FutureTask, we can write a method which does long computation but returns immediately. Those methods, instead of returning a result, return a Future object. We can later get the result by calling Future.get() method, which will return an object of type T, where T is what Future object is holding.

Future and FutureTask are available in java.util.concurrent package from Java 1.5. Future is interface and FutureTask is an implementation.

FutureTask<V> extends Object implements RunnableFuture<V>
public interface RunnableFuture<V> extends Runnable, Future<V>

Example of Future and FutureTask
Working with Thread pools is the best example of Future. When we submit a long running task to ExecutorService, it returns a Future object immediately. This Future object can be used to query task completion and getting the result of computation.

In below-given example, ExecutorService returns a Future object, which holds long value, the return type of call method in our case. Later, we check whether the task is completed or not using isDone() method. From the output, we can see that main thread returns immediately. Since we have used get() method once the task is completed, it doesn't block and return the result immediately.

By the way, the Future object returned by the submit() method is also an instance of FutureTask.

package com.thread;

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;

/**
 * Java program to show how to use Future in Java.
 * Future allows to write asynchronous code in Java, where
 * Future promises result to be available in future.
 **/
public class FutureTaskTest {

     private static final ExecutorService executorPool = Executors.newFixedThreadPool(3);
    
     public static void main(String args[]) throws InterruptedException, ExecutionException {
          
           FibonacciCalculator task = new FibonacciCalculator(15);
          
           System.out.println("Submitting Task ...");
           Future<Long> future = executorPool.submit(task);
          
           System.out.println("Task is submitted");

           while (!future.isDone()) {
                System.out.println("Task is still running... isDone()");
               
                /* sleep for 1000 millisecond before checking again */
                Thread.sleep(1000);
           }
          
           System.out.println("Task is completed, result is");
           long fibonacci = future.get();
          
           System.out.println("Fibonacci value of 100 is : get() " + fibonacci);
          
           /* Shut down the executor service. */
           executorPool.shutdown();
     }
    
     /**
      * Callble Task
      * @author rajesh
      */
     private static class FibonacciCalculator implements Callable<Long> {
          
           private final int number;
           public FibonacciCalculator(int number) {
                this.number = number;
           }

           @Override
           public Long call() {
                long output = 0;
                try {
                     output = fibonacci(number);
                } catch (InterruptedException ex) {
                     System.err.println("error");
                }
                return output;
           }

           private long fibonacci(int number) throws InterruptedException {
                if (number < 0) {
                     throw new IllegalArgumentException("Non zero number is required.");
                }
               
                long result = 1;
               
                /* Adding delay for example. */
                Thread.sleep(2);
               
                if(number==0 || number==1 || number==2) {
                     result = number;
                } else {
                     result = fibonacci(number-1)+fibonacci(number-2);
                }
                return result;
           }
     }
}


Key points Future and FutureTask

1. Future is a base interface and defines an abstraction of an object which promises result to be available in future while FutureTask is an implementation of the Future interface.

2. Future is a parametric interface and type-safe (=using generics) written as Future<V>, where V denotes value.

get():
3. Future provides get() method to get a result, which is blocking method and blocks until the result is available to Future.

cancel():
4. Future interface also defines cancel() method to cancel the task.

isDone() and isCancelled():
5. isDone() and isCancelled() method is used to query Future task states.
isDone() returns true if the task is completed and a result is available to Future. If you call get() method, after isDone() returned true then it should return immediately. On the other hand, isCancelled() method returns true, if this task is canceled before its completion.

6. There are four subinterfaces of Future, each with additional functionality e.g. Response, RunnableFuture, RunnableScheduledFuture, and ScheduledFuture. RunnableFuture also implements Runnable and successful finish of run() method cause completion of this Future.

FutureTask and SwingWorker:
7. FutureTask and SwingWorker are two well-known implementations of the Future interface. FutureTask also implements RunnableFuture interface, which means this can be used as Runnable and can be submitted to ExecutorService for execution.

8. Though most of the time ExecutorService creates FutureTask for us, i.e. when we submit() Callable or Runnable object. We can also create it manually.

9. FutureTask is normally used to wrap the Runnable or Callable object and submit them to ExecutorService for asynchronous execution.


Related Posts Plugin for WordPress, Blogger...