This week's book giveaway is in the JavaScript forum.
We're giving away four copies of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js and have Paul Jensen on-line!
See this thread for details.
Win a copy of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Unexpected death of a thread  RSS feed

 
Kelley Koszegi
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,
I am writing a class (A) that holds a hashtable of connections to a database. A separate class that extends thread (B) will periodically wake up and check each connection. If a connection has not been used for a certain amount of time, B will close it. Here is my question: how can I make sure that the B thread continues to run? If it dies unexpectedly, I want to be able to restart it. Is there some way to accomplish this? I've searched the archives but I can't seem to find what I am looking for.
Thanks,
--Kkoszegi
 
Peter den Haan
author
Ranch Hand
Posts: 3252
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sounds like that watchdog-type Thread really wants to be a TimerTask (JDK 1.3+ assumed). Have a look.
If you're really worried about threads dying, you can catch Throwable at a suitable point. I would actually suggest catching Error separately and simply logging and re-throwing these.
- Peter
[ March 25, 2002: Message edited by: Peter den Haan ]
 
Kelley Koszegi
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
We are still using version 1.2.2 of the JDK. There are no plans to upgrade, so Timer/TimerTask are not options for me.
I'm not sure I understand what you said about catching errors and exceptions. Can you (or someone else) clarify what to do? Which class catches the error? Can class A do that?
Thanks,
--KKoszegi
 
Zakaria Haque
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
the idea you handle any situation which can end the thread, for simplicity's sake you can do something like these in run method
public void run() {
try {
... loop
}catch(RuntimeException ex) {
.. you get the runtime exceptions here, just logging may suffice
}catch(Exception ex) {
.. you will get the checked exception here, just logging may suffice
}catch(Error err) {
.. you get the errors here, you should log and rethrow it
}

}
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!