user3876801
user3876801

Reputation: 243

How to terminate callable process called by ExecutorService

In the following code I want to terminate the Callable process submitted by ExecutorService. Currently the execution of the callable process is not terminating even though the shutdown called before the loop execution.

Any suggestion would be helpful.

package foundation.util.sql.parser;

import java.io.IOException;
import java.util.HashMap;
import java.util.concurrent.*;

public class Test {

   public static void main(String[] args) {
        try {

            final java.util.Map<String, ExecutorService> map = new HashMap<>();
            ExecutorService service = Executors.newFixedThreadPool(1);
            map.put("1", service);
Thread thread = new Thread(new Runnable() {

                @Override
                public void run() {
                    try {

                        System.out.println("Termination Initiated");
                        ExecutorService executorService = map.get("1");
                        System.out.println("ShutDown called");

                        if(!executorService.isShutdown())
                        {
                            executorService.shutdownNow();
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            });
            Future<Boolean> submit = service.submit(new Callable<Boolean>() {

                @Override
                public Boolean call() throws Exception {
                    int j = 0;
                    System.out.println(Thread.currentThread().getName());
                    for (int i=0; i<5000;i++) {
                        //Some business Process.
                        j = i;
                    }
                    System.out.println("Test____"+ j);
                    return null;
                }
            });
           thread.start();
           submit.get();
        } catch (Exception e) {
           e.printStackTrace();
        }
    }

}

Upvotes: 1

Views: 2231

Answers (1)

When we call showDownNow() it doesn't terminate the running tasks, in fact

it just prevents waiting tasks from starting and attempts to stop currently executing tasks.

As per javadoc

There are no guarantees beyond best-effort attempts to stop processing actively executing tasks. For example, typical implementations will cancel via Thread.interrupt(), so any task that fails to respond to interrupts may never terminate.

In your callable you are not responding/checking for the interrupts. You need check periodically if the interrupt flag is set to true. If so, do the necessary clean up if needed and terminate.

As an example, in your case you can consider checking the interrupt flag as below (or wherever applicable):

for (int i=0; i<5000;i++) {
    //Some business Process.
    if(Thread.currentThread().isInterrupted()) {
     // do any cleanup and return from here.
     return false;
    }
    j = i;
}

Upvotes: 1

Related Questions