• 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

can we stop the user created threads on application shutdown?

 
Ranch Hand
Posts: 57
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi

I created a thread in load on start up servlet, which is keep on running even i stop the application deployed in application server (Like WebSphere).

When I restart the application (but not whole server) once again new thread is creating in this servlet.

I would like to stop the thread on application shutdown or should not start second time on restarting the application.

My Thread Class


My Servlet


I tried to interrupt the thread in destroy method of servlet, but not succeed. Please help on this. thank you.
 
Saloon Keeper
Posts: 27752
196
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It is very important that any threads that you startup in a webapp be stopped when the webapp shuts down. Failure to do so may prevent the server JVM from terminating (since a JVM cannot halt until all threads are stopped), and at a minimum, the server will probably whine at you.

So you are doing the right thing and the servlet's destroy() method is the place to do it, if you start the thread in init(). However, these days, the recommended place for starting and stopping application threads is in a ServletContextListener.

Ideally, your thread will have a notification mechanism, which can be something as simple as a member variable named "stopMe" that can be set to tell the thread to collect all its toys, put them away, and exit. If the thread can't be shut down that way, then the thread cancel() method may be used to brute-force terminate it.
 
Greenhorn
Posts: 22
Oracle Ruby Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Though just a convention,a thread's run method should be written in a way that it is capable of completing the execution on it's own i.e it should have the provision to terminate on its own rather than force stopping.But that might not be possible every time.
 
Author and all-around good cowpoke
Posts: 13078
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Now I am not up to speed with the latest Java SDK - when did Thread get a cancel() method?  I see there is one in Timer.

Java 1.2 had lots of Thread methods that were determined to be dangerous because they could leave the JVM in an undefined state. See the JavaDocs for discussion of why destroy, suspend, stop, and resume are deprecated.

Bill
 
Marshal
Posts: 79151
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

William Brogden wrote:Now I am not up to speed with the latest Java SDK - when did Thread get a cancel() method?  I see there is one in Timer. . . .

What cancel method? I couldn't find one. That Timer method might stop its thread by making a flag turn false.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic