0

enter image description here Threads are not stopped even after calling Interruption from Executors shutdownNow method.
Thread Call functionality is running in a while loop , which is checking for Interruption flag. I tried sending Interruption flag to the running thread after a certain period, but it is still executing.I want to force stop the thread. Can anybody tell this behavior. Attaching the sample Java code:

public class TestExecutor {


static volatile int j = 1; 

public static void main(String[] args) {

    ExecutorService pool = Executors.newFixedThreadPool(5);
    for (int i = 1; i <= 10; ++i) {

        Future<Map<String, List<String>>> abc =   pool.submit(new Callable<Map<String, List<String>>>() {
        volatile boolean abortFlag = false;
        @Override
        public Map<String, List<String>> call() throws Exception {


            while(!abortFlag){
                long start = System.currentTimeMillis();
                for(int k=0; k < 10e4 ;k++){
                    abortFlag = abort();
                    System.out.println(Thread.currentThread().getName() +" " +abortFlag);
                }
                System.out.println("counter val is:" +Thread.currentThread().getName()  +" : "  +j++);
                long end = System.currentTimeMillis();
                System.out.println("time for one execution : "  +" "  +Thread.currentThread().getName()  +" :" +(end-start));
                return null;
            }

            return null;
        }
        private boolean abort() {

            if(Thread.currentThread().isInterrupted()){
                return true;
            }else{
                return false;
            }

        }
    });
    }
    pool.shutdown();

    try {
        if (pool.awaitTermination(3000, TimeUnit.MILLISECONDS)) {
            System.out.println("task completed");
        } else {
            System.out.println("Forcing shutdown...");
            pool.shutdownNow();
        }
    } catch (InterruptedException e) {
        e.printStackTrace();
    }


System.out.println("Closed");


}

} `

Rohit Narang
  • 55
  • 1
  • 12
  • Possible duplicate of [How to stop the execution of Executor ThreadPool in java?](https://stackoverflow.com/questions/1562079/how-to-stop-the-execution-of-executor-threadpool-in-java) – alain Sep 19 '17 at 11:38
  • 3
    Works fine here. The program stops. – JB Nizet Sep 19 '17 at 11:41
  • @JB : I have posted the Image also where you can see it is still running after the abort flag is true. Please see in edit. – Rohit Narang Sep 19 '17 at 12:04
  • And the image shows that the program ends up stopping. If you're wondering why it doesn't stop as soon as the abort flag becomes true, then it's simply because your for loop only stops after 10e4 iterations. – JB Nizet Sep 19 '17 at 12:42
  • Just a side note, Don’t fill your code with noise like `if(Thread.currentThread().isInterrupted()){ return true; }else{ return false; }`. Just write `return Thread.currentThread().isInterrupted();` or even better `return Thread.interrupted();` which is intended for checking your own interruption status (but clears it at the same time). – Holger Sep 19 '17 at 14:21
  • @JB: Basically what i have to do in my application is to search data from logs in different foldres , but if in case it is searching in a vast directory it end up doing in searching the entire directory so at that point i want to terminate these tasks and stop the further execution after a certain period of time. – Rohit Narang Sep 19 '17 at 17:41

2 Answers2

1

You have a 'long' execution before you act on abortFlag in while(!abortFlag). As per the loop below, you will have up to 10k print outs before you have opportunity to exit. If your application can exit before finishing that 'long' task, then just exit the inner loop when the flag has been toggled:

for(int k=0; k < 10e4 ;k++){
    abortFlag = abort();
    if (abortFlag) { 
        break;
    }
    System.out.println(Thread.currentThread().getName() +" " +abortFlag);
}
diginoise
  • 7,352
  • 2
  • 31
  • 39
1

shutDown() , will wait for all the active/queued tasks to complete and will not allow any new task to be submitted.

shutDownNow() , will not allow any new task and tasks still in the queue will not be run and currently running tasks/threads will be interrupted.

So, The reason why the threads where still running even after shutdownNow is called is because threads still in the for loop will still be running since no action is taken on the abortFlag. in the for loop.

Change Code Snippet :

 abortFlag = abort();
 if(abortFlag){
   System.out.println(Thread.currentThread().getName()+" K="+k);
   break;
 }

Thanks Roshan

Roshan
  • 667
  • 1
  • 5
  • 15