• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

should shutdownNov method of thread pool be applied after shutdown method to avoid threadpool hangin

 
Ranch Hand
Posts: 2925
13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have specified timeperiod of 120 minutes in threadpool.shutdown method.Despite this the java process does not exit at times and hangs for many hours. I can check this in unix by ps fe | grep command /

Is the below way correct to avoid the possibility of java not exiting:

Use shutdownNov method just after shutdown method.
use System.exit just after this to ensure that java process exits.

thanks
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
About what class exactly are you talking, I guess you mean java.util.concurrent.ThreadPoolExecutor?

When you call shutdown(), then that will not immediately stop all the threads that are running in the ThreadPoolExecutor. It only means that it will stop accepting new tasks, but the tasks that are currently running will continue to run. I don't know what your threads are doing, but if they are running long-running tasks, then you will have to implement your own mechanism to tell those tasks to stop.

When you call shutdownNow(), the thread pool will attempt to stop all running tasks by interrupting the tasks. If a task running in one of the threads is blocked in some blocking operation (for example waiting for input), then an InterruptedException will happen in the thread. The task then has to make sure that it stops.

So, it's not just a matter of calling shutdown() or shutdownNow(). You also have to make sure that your tasks running in the thread pool will stop if they are asked to stop, otherwise they will just keep on running.

Normally you would do something like this to shutdown the ThreadPoolExecutor:

 
Monica Shiralkar
Ranch Hand
Posts: 2925
13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks.

Yes I am talking about java.util.concurrent.ThreadPoolExecutor.

I don't know what your threads are doing, but if they are running long-running tasks, then you will have to implement your own mechanism to tell those tasks to stop.





I am the below method. Will it not ensure that thread will be closed java process will be ultimately killed?

 
Jesper de Jong
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Just calling shutdown() and awaitTermination() will not stop the threads and will not by itself stop and exit the JVM process. You will have to implement your own mechanism to stop the threads.

What are the threads doing? They are probably doing long-running tasks. You probably have some kind of loop in the run() method, like this:

You should implement a mechanism to tell the task to stop:

Then call the stop() method from another thread to make the task return from the run() method.
 
Monica Shiralkar
Ranch Hand
Posts: 2925
13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Just calling shutdown() and awaitTermination() will not stop the threads and will not by itself stop and exit the JVM process.



Then what will specifying the parameter 120, TimeUnit.MINUTES do?

thanks
 
Jesper de Jong
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It will simply wait for at most 120 minutes until the tasks have stopped. But it is your job to stop the tasks.
 
Monica Shiralkar
Ranch Hand
Posts: 2925
13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

It will simply wait for at most 120 minutes until the tasks have stopped.



What exactly will happen after its wait of 120 minutes is over?
 
Jesper de Jong
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
awaitTermination returns, and if all the tasks have stopped it will return true, and if the tasks have not been stopped it will return false.

Note that this is all exactly documented in the API documentation of ThreadPoolExecutor.
 
Monica Shiralkar
Ranch Hand
Posts: 2925
13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks
 
reply
    Bookmark Topic Watch Topic
  • New Topic