Thread stopping

HI, I have a question regarding the use of threads in my program.

I have 4 classes which I have implemented as Runnable. Each class uses it's run method to call methods which carry out queries on a database. I have a thread t which I use to run the methods.

publicvoid run()

{

setTotalIncoming();

getTotalOutbound();

setTotalLostCalls();

setTotalCallsSentToVoicemail();

getOutOfHoursVoicemail();

setPercentAnswered();

getTotalPhoneTime();

getTotalTalkTime();

setAverageAnswerTime();

setLostRingingTime();

setVoicemailRingingTime();

getOutOfHoursRingingTime();

}

The problem is that periodically (unspecified time periods), my program calls the refresh() method in my program which re-assigns my thread t. Shown here:

publicvoid refresh()

{

while(isRunning){

// wait for thread to finish

}

t =new Thread(this);

t.start();

}

Now, the problem is that, currently, my program could re-assign t, whilst the run() method wasn't finished executing all it's methods. I think this is causing some unexpected behaviour in my code.

I am noticing this in my ConnectionPool class, which I know is sound, as it is a very similar adaptation on code found in a well known book. Basically my program manages to open more connections that the Maximum no. of connections is set to in the ConnectionPool.

Does anyone have any ideas:

1. How I can ensure that all my methods have finished executing, before I allow the refresh() method to re-assign t.

2. If this problem could cause my connection pool to open more connections than i have set the maximum to.

Thanks in advance for your time, I hope someone can help me!

[2216 byte] By [djroj2001a] at [2007-10-3 4:45:25]
# 1
Sorry, I should just mention:My refresh() method no longer has the while loop, as I couldn't find away to make sure fulle execution of each method had finished.
djroj2001a at 2007-7-14 22:49:42 > top of Java-index,Core,Core APIs...
# 2

> public void refresh()

> {

>while(isRunning){

> // wait for thread to finish

>}

>t = new Thread(this);

>

> }

> Now, the problem is that, currently, my program could

> re-assign t, whilst the run() method wasn't finished

> executing all it's methods.

Well, you could change the above loop to while (t.isAlive())

> some unexpected behaviour in my code.

> 1. How I can ensure that all my methods have finished

> executing, before I allow the refresh() method to

> re-assign t.

You could use a java.util.concurrent.CountDownLatch so that your loop would wait on a latch (released at the end of the run() method) before re-assigning t. Or even just synchronize on some common object - let me know if you need an example.

> 2. If this problem could cause my connection pool to

> open more connections than i have set the maximum

> to.

Well, it certainly is bad logic and so may well be the reason that you are opening more connections than you're closing

oxbow_lakesa at 2007-7-14 22:49:42 > top of Java-index,Core,Core APIs...
# 3
Use Thread.join() to wait for a thread to finish.
davidholmesa at 2007-7-14 22:49:42 > top of Java-index,Core,Core APIs...
# 4
> Use Thread.join() to wait for a thread to finish.D'Uh (slaps himself on forehead)
oxbow_lakesa at 2007-7-14 22:49:42 > top of Java-index,Core,Core APIs...
# 5
> > Use Thread.join() to wait for a thread to finish.> > D'Uh (slaps himself on forehead)a cup of coffee could have helped
kilyasa at 2007-7-14 22:49:42 > top of Java-index,Core,Core APIs...