ScheduledExecutorService and ThreadPoolTaskExecutor that interrupts tasks after a timeout

元气小坏坏 提交于 2020-01-17 03:44:14

问题


I Used ExecutorService that interrupts tasks after a timeout.I use a ScheduledExecutorService for this. First I submitted the thread and it once to begin immediately and retain the future that is created. After that i use ScheduledExecutorService as a new task that would cancel the retained future after some period of time.

//Start Spring executor to submit tasks
ThreadPoolTaskExecutor taskExecutor = (ThreadPoolTaskExecutor) ApplicationContextProvider.getApplicationContext().getBean("taskExecutor"); 

CompletionService completionService = new ExecutorCompletionService(taskExecutor);
//End Spring executor to submit tasks

// Start ScheduledExecutorService  to submit returned future object to timeout

ScheduledExecutorService executor = Executors.newScheduledThreadPool(Integer.parseInt(config.getProperty("DBPOLLER_COREPOOLSIZE")));

final Future<String> future = completionService.submit(batchJob); // Submit actual task and get future

// submit future

executor.schedule(new Runnable() {
      public void run() {
                         future.cancel(true);
      }
   }, dbPollerTimeOut, TimeUnit.MINUTES);

int count = taskExecutor.getActiveCount();

 if (count == 0) {

                taskExecutor.shutdown();
                executor.shutdown();
                finalExitStatus = 0;                    
                break;

            } 

I have implemented the solution which is in below url: ExecutorService that interrupts tasks after a timeout, it was working fine, until timeout, but once timeout happens, it cancels all theenter code here tasks i ThreadPool which is not acceptable. I need to cancel only tasks that are long running and reach timeout.

Any idea how to achieve this?


回答1:


It is not clear what your CompletionService is, and you are submitting your batchJob on it, so it is hard to tell exact root cause of your problem. But ideal scenario of submitting few tasks and cancelling them after some time, is to use ScheduledExecutorService for both purposes.

So, can try submitting the batchJobon instance of ScheduledExecutorService i.e. executor.

final Future<String> future = executor.submit(batchJob); // Submit actual task and get future       

EDIT UPDATE: Important change you SHOULD do in your code
I see that you are never stopping your ScheduledExecutorService which is wrong because resources it occupies will never be released until you stop it. So, your updated code should be as below:

ScheduledExecutorService executor = Executors.newScheduledThreadPool(Integer.parseInt(config.getProperty("DBPOLLER_COREPOOLSIZE")));
final Future<String> future = executor.submit(batchJob); // Submit actual task and get future
executor.schedule(new Runnable() {
      public void run() {
        future.cancel(true);
        executor.shutdownNow();
      }
   }, dbPollerTimeOut, TimeUnit.MINUTES);


来源:https://stackoverflow.com/questions/30649643/scheduledexecutorservice-and-threadpooltaskexecutor-that-interrupts-tasks-after

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!