Saturday 8 March 2014

Future : cancel task execution

Future interface provides a method to cancel the task.

boolean cancel(boolean flag)
Method attempts to cancel the task. If the task already completed then this attempt will fail. If the flag is true then the thread executing this task should be interrupted; otherwise, in-progress tasks are allowed to complete

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){
            System.out.println("Task Interrupted " + 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 task1 = new CallableTask("task1");
               
               Future<String> f1 = exec.submit(task1);
                Thread.sleep(100);
               System.out.println("cancel method returned " + f1.cancel(true));
              
               exec.shutdown();
 }
}

Output
task1 Started Executing
cancel method returned true
Task Interrupted java.lang.InterruptedException: sleep interrupted

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 task1 = new CallableTask("task1");
               
               Future<String> f1 = exec.submit(task1);
                Thread.sleep(100);
               System.out.println("cancel method returned " + f1.cancel(false));
              
               exec.shutdown();
 }
}

Output
task1 Started Executing
cancel method returned true
task1 Finishes Execution

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.

Some Points to remember
1. If the cancel method unable to cancel the task, then the cancel method returns false.

Get the result of the task                                                 Check the completion status of task                                                 Home

No comments:

Post a Comment