• Post Reply Bookmark Topic Watch Topic
  • New Topic

not able to stop thread  RSS feed

 
dhanunjaya kumar
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi ,
i am continuosly reading mails from pop3 server.
to read continuosly i am using a thread with infinite loop.
while running in the tomcat it works fine .
when i shut down the tomcat i am getting an error like "failed to shut down".
this was caused due to the thread.this thread never dies off.
can any one suggest a good logic or the procedure to kill the thread while shut down of tomcat.
 
Ulf Dittmer
Rancher
Posts: 42970
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You might have a look at java.lang.Runtime.addShutdownHook, which lets you perform any kind of cleanup whenever the JVM is shut down. You can place code there that terminates the mail thread.

A better way may be to use a ServletContextListener, and perform the cleanup in the contextDestroyed method. That gets executed whenever the web app is un-/reloaded (while the above mechanism doesn't).
 
Srikanth Raghavan
Ranch Hand
Posts: 389
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, this is a common problem with infinitely looping threads and you gotta have a good design to accomadate a shutdown or stop facility. A little status checking would solve your problem.

As Ulf Dittmer said, a listener will be very useful.

Here's a pseudo code that may help you...



This requires careful design of the classes upfront. Please correct me if I am wrong or there is a better approach available.

Hope this helps,
Srikanth
[ October 23, 2006: Message edited by: Srikanth Raghavan ]
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!