• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

can we stop the user created threads on application shutdown?

 
kiran nyala
Ranch Hand
Posts: 55
Java
  • Mark post as helpful
  • send pies
  • 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.
 
Tim Holloway
Saloon Keeper
Posts: 18359
56
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • 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.
 
Sibendu Dey
Greenhorn
Posts: 20
Java Oracle Ruby
  • Mark post as helpful
  • send pies
  • 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.
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13077
6
  • Mark post as helpful
  • send pies
  • 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
 
Campbell Ritchie
Sheriff
Posts: 51344
86
  • Mark post as helpful
  • send pies
  • 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.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic