Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to stop all running threads in ThreadPoolTaskExecutor?

I am add new threads like this using ThreadPoolTaskExecutor :

threadPoolTaskExecutor.execute(new Runnable() {

    @Override
    public void run() {
        while(true){
            doSomething();
        }

        Thread.sleep(1000); 
    }

});

All active threads are not stopped, when I shutdown this executor:

threadPoolTaskExecutor.shutdown();

Is there option how to stop this active threads using ThreadPoolTaskExecutor?

like image 417
Green Lei Avatar asked Oct 19 '22 08:10

Green Lei


1 Answers

You should set setWaitForTasksToCompleteOnShutdown(false) when create ThreadPoolTaskExecutor.

Default is false, shutting down immediately through interrupting ongoing tasks and clearing the queue.

like image 61
codeaholicguy Avatar answered Oct 21 '22 23:10

codeaholicguy