2

I have a certain process which can be execute in a Thread. I need to stop the processing completely after certain time Example :90 seconds. I read we have an option in futuretask to set the timeout for a thread. But i am getting the timeout exception but the started task is running in the backend, it is not completely stopped on using futureTask.cancel(true) or executor.isShutdown().

I tried to split it up and test how to stop the thread completely , but even I could not stop the started thread completely, Below the sample piece of code. Using below code the callable is not returning the String after we cancel the thread, but i could see the for is not stopping on cancelling the thread.

Please help me where it is missed?

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.FutureTask;
public class FutureTaskDemo {
    public static void main(String... args) throws InterruptedException, ExecutionException{
        ExecutorService exService = Executors.newSingleThreadExecutor();
        FutureTask<String> futureTask= new FutureTask<String>(new Task());
        exService.execute(futureTask);
        //checks if task done
        System.out.println("Task Done :"+futureTask.isDone());
        //checks if task canceled
        System.out.println("Task isCancelled :"+futureTask.isCancelled());
        boolean isCancel = futureTask.cancel(true);
        System.out.println("Cancelled :"+isCancel);
        //fetches result and waits if not ready
        System.out.println("Task is done : "+futureTask.get());
    }
}
class Task implements Callable<String>{
    public String call() {
            for (int i=0; i<10000; i++){
                System.out.println(i);
            }
        return "Task Done";
    }
} 
TikTik
  • 347
  • 2
  • 8
  • 22

1 Answers1

0

The only way to stop a thread completely is to kill the process. If you want to stop the task gracefully and keep the program running your Task need to support this.

public String call() {
    for (int i = 0; i < 10000 && !Thread.currentThread().isInterrupted(); i++) {
         System.out.println(i);
    }
    return "Task Done";
}
Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
  • Thanks Peter Lawrey for your comments, But my actual use case is to stop the processing of a method, here i have just mentioned the sample code... – TikTik Mar 27 '17 at 12:17
  • @Selva never the less. The answer is no different. Either the code you call supports interrupts or early termination or you have to kill the process. – Peter Lawrey Mar 27 '17 at 13:09
  • as i requested my use case is to stop the child thread processing from the parent thread. In my child thread i am calling a method which does a hell a lot of process, here i can't check for `!Thread.currentThread().isInterrupted()` in all the business logics inside the method. Kindly let me know do we have any other feasible suggestion – TikTik Mar 28 '17 at 10:49
  • @Selva you could use byte code injection to add these checks or alter a library it calls regularly to do the check. – Peter Lawrey Mar 28 '17 at 15:13