वरुण
वरुण

Reputation: 1725

How to shut down all threads in executor service immidiately?

I have used shutDown() and shutDownNow(), but both method does not stop all threads immediately. shutDownNow() is more preferable among these two but it waits for the running thread to complete it's task. In my scenario I have a huge task dealing with postgres database and I want to shut down that thread immediately without waiting for the completion of execution.

What is the way to shut down the all the threads immediately?

Upvotes: 0

Views: 3397

Answers (4)

Mananpreet Singh
Mananpreet Singh

Reputation: 295

shutdownNow : Attempts to stop all actively executing tasks, halts the processing of waiting tasks, and returns a list of the tasks that were awaiting execution. This method does not wait for actively executing tasks to terminate. Use awaitTermination to do that.

shutdown : Initiates an orderly shutdown in which previously submitted tasks are executed, but no new tasks will be accepted. Invocation has no additional effect if already shut down. This method does not wait for previously submitted tasks to complete execution. Use awaitTermination to do that.

http://docs.oracle.com/javase/8/docs/api/java/util/concurrent/ExecutorService.html#shutdown%28%29

or you can see this :

You can use an ExecutorService instead which combines a thread pool with a queue of tasks.

 ExecutorService service = Executors.newCachedThreadPool();
 // or
 ExecutorService service = Executors.newFixedThreadPool(THREADS);

 // submit as many tasks as you want.
 // tasks must honour interrupts to be stopped externally.
 Future future = service.submit(new MyRunnable());

 // to cancel an individual task
 future.cancel(true);

 // when finished shutdown
 service.shutdown();

Upvotes: 1

Buhb
Buhb

Reputation: 7149

Depending on how you call the database, you could try to explicitly cancel the query. Se related question.

Upvotes: 0

Ravindra babu
Ravindra babu

Reputation: 38950

I doubt that it's possible in safe way.

The safe way to shutdown the executor service as per oracle documentation

 void shutdownAndAwaitTermination(ExecutorService pool) {
   pool.shutdown(); // Disable new tasks from being submitted
   try {
     // Wait a while for existing tasks to terminate
     if (!pool.awaitTermination(60, TimeUnit.SECONDS)) {
       pool.shutdownNow(); // Cancel currently executing tasks
       // Wait a while for tasks to respond to being cancelled
       if (!pool.awaitTermination(60, TimeUnit.SECONDS))
           System.err.println("Pool did not terminate");
     }
   } catch (InterruptedException ie) {
     // (Re-)Cancel if current thread also interrupted
     pool.shutdownNow();
     // Preserve interrupt status
     Thread.currentThread().interrupt();
   }
 }

If you really want to follow a crude way, I will suggest a solution which may not be 100% accurate and I don't recommend personally. I would like to use ExecutorService methods instead of this crude solution.

  1. Create your own thread and thread pool as per this article
  2. Add one more boolean in Worker thread - boolean runNow = true;
  3. run method of Worker thread will be like this : while ( runNow) { // your logic }
  4. When you want to shut down all the threads, add one more method in ThreadPoolManager. Iterate through myQueue and interrupt all Runnables. Catch that interrupted exception and make the boolean runNow as false.

Upvotes: 0

Adrian Duta
Adrian Duta

Reputation: 81

The single "clean" way to stop the threads is, if you have some loops inside, to stop the loop through some boolean variable like "stopThread", and you have to handle the variable.

Example:

public void run(){

for(int i=0; i<1000000 && (!stopThread) ; i++){

// do something

} }

Upvotes: 0

Related Questions