Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Cancelling thread on shutdown  RSS feed

 
Oliver Kohll
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello, I have a servlet application that starts a new thread on initialisation to do some database stuff.



It could potentially take up to an hour to complete.

For debugging purposes, I'd like to be able to shut down the app shortly after starting, before dashboardPopulator has finished its work. That object will deal with an InterruptedException gracefully, closing database connections etc.

The question is, what's the best way to do this. The options I'm considering are:

1) Making the thread a daemon before starting it with


However the Thread JavaDoc says that 'the Java Virtual Machine exits when the only threads running are all daemon threads' which doesn't sound like it interrupts the thread, but just quits immediately.

2) Make initialDashboardPopulatorThread a field in the servlet. I could then call

in the servlet's destroy() method.

However it seems slightly unnecessary to have the Thread variable declared for the life of the servlet if it's only going to be executed once.

Any comments?

Regards
Oliver
 
Steve Luke
Bartender
Posts: 4181
22
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oliver Kohll wrote:
1) Making the thread a daemon before starting it with


However the Thread JavaDoc says that 'the Java Virtual Machine exits when the only threads running are all daemon threads' which doesn't sound like it interrupts the thread, but just quits immediately.



That's right, shutting down won't interrupt daemon threads. And the daemon threads will only end when the JVM housing them does - in a web app that may mean the entire servlet container, not just the particular web app.


2) Make initialDashboardPopulatorThread a field in the servlet. I could then call

in the servlet's destroy() method.

However it seems slightly unnecessary to have the Thread variable declared for the life of the servlet if it's only going to be executed once.



Maybe a tiny bit of extended scope, but there is little or no problem with it. I would probably put it in a ServletContextListener rather than the Servlet init()/destroy() method, since I think that is more of where the process belongs (at least to me it sounds that way - once per app, start up when app begins and shutdown, at latest, when app ends). If you are worried about the extra Thread object hanging around longer than necessary you could add a 'listening' component to it (The Servlet (Context) passes itself to the dashboardPopulator and when the dashboardPopulator completes its task it informs the Servlet which then nullifies the Thread reference so it can be garbage collected.)
 
Oliver Kohll
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Good plan, I'll give it a go. Thanks
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!