• Post Reply Bookmark Topic Watch Topic
  • New Topic

ScheduleExecutorService and stopping threads

 
Mike Bates
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am playing around with this sample code for the ScheduleExecutorService. The code works, the future task gets canceled but the threads do not end. So on Windows (I have not tried this on Linux) the program keeps running. I think I need to add in shutdownNow but have not figured out where that would be added, thought may be as another scheduled task, but I think once the tasks are completed either due to a cancel or interrupt the program should end cleanly.

Some guidance on where I should look or do next. I am waiting for two books on concurrency and hope to get one on threads this weekend.

Mike


 
Adam Smolnik
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey.

...where that would be added

It depends on your design, the sort of your application (desktop/servlet/other) and other requirements.
Usually you should insert scheduler.shutdown(); into the common cleaning resource method/place of your program.
If you don't switch off the scheduler explicitly, it will be waiting for the next task to process.

...but I think once the tasks are completed either due to a cancel or interrupt the program should end cleanly

Not necessarily. The ExecutorService is a framework to execute tasks on your demand.
It doesn't know when you decide to order a new task during life cycle of your program. So it is still waiting for this moment after starting actual work of thread/threads.

Adam
 
Mike Bates
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Adam your response.

I'll work on my framework for threading. My goal is to develop the app such that a primary thread kicks of and will ultimately terminate when a .pid file is deleted (probably a deamon style process.) The other threads will be scheduleExecutorService schedule threads that run every 1 minute, every 5 minutes to every hour. One or two of them run about 20 seconds to complete their work. So when the primary thread terminates, the scheduler should shutdown and the other threads should stop after completing their tasks.

So with the testing with the code at the top of the thread I was trying to figure out how to get the program to terminate cleanly without doing a control-c since when I do the cancel the program was not stopping. So I am trying to figure out how to shutdown this scheduler so it does not hang is what you are telling me in this case.

Thanks
Mike
 
Adam Smolnik
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey.

Take a look at my proposals of the solution - I hope I understand your intention properly.
The first example is somewhat "brutal", simply invoking scheduler.shutdown() after cancelling tasks.
It seems to work well but I'm not sure whether it will indeed work correctly - this is a sort of "self-closing".
I have to consider this way in details yet because I'm afraid some unexpected after-effects. I strive to be as watchful as a hawk during concurrence programming.

But the latter is based on creation of my own cleaning service. It is the simplest but a bit more verbose example.

1.



2.



Adam
 
Mike Bates
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Okay, I certainly did not think to put the shutdownNow in the same piece of code as the cancel. I was trying several other things along with shutdownhooks.

I like your clean up solution. The first solution got me to thinking that if in my scenario that once the .pid file is remove the daemon thread could schedule the cancel and then the shutdownNow as you did in the Beeper solution. I could even spawn a cleanup thread at that point.

Now to absorb you solution which I did not expect.

Thanks
Mike
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!