简体   繁体   English

如何终止ExecutorService调用的可调用过程

[英]How to terminate callable process called by ExecutorService

In the following code I want to terminate the Callable process submitted by ExecutorService. 在以下代码中,我想终止ExecutorService提交的Callable流程。 Currently the execution of the callable process is not terminating even though the shutdown called before the loop execution. 当前,即使在循环执行之前调用了shutdown,可调用过程的执行也不会终止。

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();
        }
    }

}

When we call showDownNow() it doesn't terminate the running tasks, in fact showDownNow()当我们调用showDownNow()它不会终止正在运行的任务

it just prevents waiting tasks from starting and attempts to stop currently executing tasks . 它只是防止等待的任务开始,试图停止当前正在执行的任务

As per javadoc 根据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. 例如, 典型的实现将通过Thread.interrupt()取消,因此任何无法响应中断的任务都可能永远不会终止。

In your callable you are not responding/checking for the interrupts . 在您的可呼叫电话中,您没有响应/检查中断 You need check periodically if the interrupt flag is set to true. 您需要定期检查中断标志是否设置为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): 例如,在您的情况下,您可以考虑如下检查interrupt flag (或任何适用的地方):

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;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM