object wait(timeout) not timing out
2 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   Glenn_Wiebe
Posted On:   Wednesday, August 29, 2001 11:25 AM

I have a main thread that creates an object with a Runnable interface, creates a thread out of it and then starts it. The main thread then waits for 180 seconds on the object. If after the timeout period, the object has not done its work, I want to stop the old object, create a new one and try again. Unfortunately the wait(180000) never times out!!! Thanks for any help. Glenn Consumer: RptGetter rmGet = new RptGetter(mSid, mRptMgr, mRptId, mKv, kTagList, mDebugLevel); Thread rgt = new Thread(rmGet); rgt.start(); synchronized(rmGet) { rmGet.wait(180000); if (!rmGet.done) { // NOT DONE - stop the thread rg   More>>

I have a main thread that creates an object with a Runnable interface, creates a thread out of it and then starts it. The main thread then waits for 180 seconds on the object. If after the timeout period, the object has not done its work, I want to stop the old object, create a new one and try again.


Unfortunately the wait(180000) never times out!!!

Thanks for any help.


Glenn


Consumer:


			
RptGetter rmGet = new RptGetter(mSid, mRptMgr, mRptId, mKv, kTagList, mDebugLevel);
Thread rgt = new Thread(rmGet);
rgt.start();

synchronized(rmGet) {
rmGet.wait(180000);

if (!rmGet.done) {
// NOT DONE - stop the thread
rgt.stop();
} else {
// DONE - get report and continue
mTds = rmGet.getTDS();
gotRpt = true;
} // end else if done




Producer:


			
class RptGetter implements Runnable {
public void run() {
synchronized (this) {
tds = rptMgr.getReport
done = true;
this.notifyAll();
} // end synchronized code
} // end run method


   <<Less

Re: object wait(timeout) not timing out

Posted By:   Alex_Chaffee  
Posted On:   Thursday, September 27, 2001 08:27 AM

I'm not sure, but I think the problem is that the running object never releases the lock. The code

synchronized (this) {
tds = rptMgr.getReport
done = true;
this.notifyAll();
} // end synchronized code

should be replaced with

tds = rptMgr.getReport
synchronized (this) {
done = true;
this.notifyAll();
} // end synchronized code

That way the thread manager can interrupt the processing of getReport(), and can give the lock back to the waiting thread when the timeout expires.


Wait and notify are tricky. Please review How do wait and notify really work? until you understand my answer.

Re: object wait(timeout) not timing out

Posted By:   Anonymous  
Posted On:   Wednesday, August 29, 2001 09:40 PM

Mathias Neuhaus has an excellent answer on this before but I could not locate it. The gist is that your wait and your starting of your thread is not within the same synchronization block. This is prone to timing errors.


On another issue, you are saying that you want to create a new Runnable and reassociate this to your existing Thread? Your current design won't work - you need to create your own thread class.


Only my 2 cents :-)

About | Sitemap | Contact