Saturday 8 March 2014

Future : get(long timeout, TimeUnit unit) : get the result of task

V get(long timeout, TimeUnit unit) throws InterruptedException, ExecutionException, TimeoutException
function returns the result from the given task. If the task still computing the result, then this method waits until the timeout period finishes. Once the task finishes it gets the result. Get() throws InterruptedException if the current thread was interrupted while waiting, throws ExecutionException if the computation threw an exception and throws TimeoutException, if the wait timed out

import java.util.concurrent.*;
import java.util.*;

public class CallableTask implements Callable{
    String name;
    CallableTask(String name){
        this.name = name;
    }
    
    public String call(){
        try{
            System.out.println(name +" Started Executing");
            Thread.sleep(1000);
            System.out.println(name +" Finishes Execution");
        }
        catch(Exception e){
            
        }
        return (new Date() + " :: " + name);
    }
}

import java.util.concurrent.*;
import java.util.*;

class SimpleThreadPool{
 public static void main(String args[])throws Exception{
  
                List<Future<String>> l1 = new ArrayList<Future<String>> ();
  ExecutorService exec = new ThreadPoolExecutor(3, 3, 0L, TimeUnit.MILLISECONDS, new LinkedBlockingQueue<Runnable>());
   CallableTask tasks[] =new CallableTask[5];
   for(int i=0; i<5; i++){
    tasks[i] = new CallableTask("task" + i);
    Future<String> future = exec.submit(tasks[i]);
                                l1.add(future);
                                try{
                                    System.out.println(future.get(100,TimeUnit.MILLISECONDS));
                                }
                                catch(TimeoutException e){
                                       System.out.println(e); 
                                        }
   }
                        exec.shutdown();
                        exec.awaitTermination(5, TimeUnit.SECONDS);
                        System.out.println("Results of the tasks are");
                        for(Future f1 : l1){
                            System.out.println(f1.get());
                        }
                        System.out.println("Execution Completed");
 }
}

Output
task0 Started Executing
java.util.concurrent.TimeoutException
task1 Started Executing
java.util.concurrent.TimeoutException
task2 Started Executing
java.util.concurrent.TimeoutException
java.util.concurrent.TimeoutException
java.util.concurrent.TimeoutException
task0 Finishes Execution
task3 Started Executing
task1 Finishes Execution
task4 Started Executing
task2 Finishes Execution
task3 Finishes Execution
task4 Finishes Execution
Results of the tasks are
Sat Mar 08 11:45:45 IST 2014 :: task0
Sat Mar 08 11:45:45 IST 2014 :: task1
Sat Mar 08 11:45:45 IST 2014 :: task2
Sat Mar 08 11:45:46 IST 2014 :: task3
Sat Mar 08 11:45:46 IST 2014 :: task4
Execution Completed


future.get(100,TimeUnit.MILLISECONDS) : Method waits for the result of the task atmost 100 milliseconds, If the wait time out, then get throws TimeoutException.

ExecutorService exec = new ThreadPoolExecutor(3, 3, 0L, TimeUnit.MILLISECONDS, new LinkedBlockingQueue<Runnable>())
Statement creates a thread pool of 3 threads.

exec.submit(tasks[i])
Submit the tasks to the thread pool. When the task is submitted, ThreadPool assigns a thread to execute the call method of the Callable Object.

Related Posts

Futute: Get result of the task                                                 Future: Cancel task execution                                                 Home

No comments:

Post a Comment