With a Java ExecutorService, how do I complete actively executing tasks but halt the processing of waiting tasks?

前端 未结 3 1087
清歌不尽
清歌不尽 2021-02-13 03:33

I am using an ExecutorService (a ThreadPoolExecutor) to run (and queue) a lot of tasks. I am attempting to write some shut down code that is as graceful as possible.

Exe

3条回答
  •  無奈伤痛
    2021-02-13 03:58

    You can wrap each submitted task with a little extra logic

    wrapper = new Runnable()
        public void run()
            if(executorService.isShutdown())
                throw new Error("shutdown");
            task.run();
    
    executorService.submit(wrapper);
    

    the overhead of extra checking is negligible. After executor is shutdown, the wrappers will still be executed, but the original tasks won't.

提交回复
热议问题