The moose likes Threads and Synchronization and the fly likes Unexpected death of a thread Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Java » Threads and Synchronization
Reply Bookmark "Unexpected death of a thread" Watch "Unexpected death of a thread" New topic
Author

Unexpected death of a thread

Kelley Koszegi
Greenhorn

Joined: Aug 20, 2001
Posts: 27
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


Kelley Koszegi<br />Sun Certified Java Programmer
Peter den Haan
author
Ranch Hand

Joined: Apr 20, 2000
Posts: 3252
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

Joined: Aug 20, 2001
Posts: 27
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

Joined: Jan 02, 2002
Posts: 60
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
}

}


tobe bondhu nouka bherao<br />shonabo gaan aj shara raat
 
 
subject: Unexpected death of a thread
 
Threads others viewed
Killing a process in sleep
where should i create the database connection
Contradictory requirements
the priority about the thread.
Thread made out of Thread
developer file tools