Discovering the death of a thread
3 posts in topic
Flat View  Flat View

Posted By:   Kelley_Koszegi
Posted On:   Monday, March 25, 2002 07:56 AM

I have two classes. One uses a hashtable to maintain a list of connections. The other extends Thread and periodically wakes up to make sure that the connections are not too old. What I would like to know is, how can I make sure that the thread continues to maintain the connection pool? In other words, if the thread unexpectedly dies, how can I detect that and create a new thread?

I have seen suggestions to use Timer and/or TimerTask, but we have not yet upgraded to JDK 1.3, so I can't use them.

Any suggestions would be appreciated.

Re: Discovering the death of a thread

Posted By:   Kumaran_N  
Posted On:   Saturday, April 6, 2002 05:08 AM

When a thread dies, it comes out of its run() method. The thread can stop in a normal or abnormal way.

So you could put a finally block in the run() method and call a listener that listens to the closing event of the Thread that maintains your pool.

This way, the thread itself notifies you before dying. If its an abnormal death, you can take necessary action.



Re: Discovering the death of a thread

Posted By:   Edward_Harned  
Posted On:   Monday, March 25, 2002 01:35 PM

What you have are two threads. One is the main thread (A); it creates a new thread (B). Both threads share a hashtable. The problem is how does thread (A) know what thread (B) is

The answer is as old as the first computer with multi-threading/tasking. It is threads talking to each other.

Set up a data structure next to the hashtable that is accessible to both threads. You say thread (B) wakes up periodically, a sleep time I assume.

long sleep_time = 10000L  // ten minutes in milliseconds

long last_awake = 0; // last time awake

Now when thread (B) awakens, it updates last_awake with the System.currentTimeMillis().

When thread (A) is curious about the health of thread (B), it can check that the last_awake (plus the sleep_time or something) is reasonable.

This is only one of MANY ways threads can talk to each other.

Re: Discovering the death of a thread

Posted By:   Tonio_Caputo  
Posted On:   Monday, March 25, 2002 12:58 PM

I haven't use too much thread's, so probably
what I'm asking is unusefull, what about
using myThread.isAlive() ?

Sorry if this question has an obvious

About | Sitemap | Contact