This week's book giveaway is in the OCMJEA forum.
We're giving away four copies of OCM Java EE 6 Enterprise Architect Exam Guide and have Paul Allen & Joseph Bambara on-line!
See this thread for details.
The moose likes Web Services and the fly likes Wait and Sleep methods Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Java » Web Services
Bookmark "Wait and Sleep methods" Watch "Wait and Sleep methods" New topic
Author

Wait and Sleep methods

Imesh Damith
Greenhorn

Joined: Jul 30, 2009
Posts: 12
Hi,
I have 2 pararell threads and T1 is waiting till T2 completes its work.

T1 is checking Database every 3 seconds (3 secs * 4 times) whether the T2 has completed its work and updated the database.
now my problem is, even though T2 has completed its work T2 has no chance to update the Database in most cases. that is due to T1 is keep on checking the database without giving a CPU chance to T2 to execute its process. so T1 says in most cases that T2 has not update the DB withing that wait time (3secs * 4 times). where as T2 already completed its job but CPU has not given a chance to run the data base update.
i have use the sleep method to wait T1 to 3 seconds.

so my poroblem is how to controll the giving chance to T2 to update the database withing that 12 seconds (3 secs * 4 times)?
I do not like to change the priority of the thread. how about if I use T1.wait(3000) ? will this solve my probelm.

Thanks in advance.
Imesh
James Ward
Ranch Hand

Joined: Apr 27, 2003
Posts: 263
Can T2 inform T1 that it is done with updating database, so that there is no need for T1 to poll the db?
Imesh Damith
Greenhorn

Joined: Jul 30, 2009
Posts: 12
well in that case T1 has to wait till T2 complete. but we can not guarantee what time T2 complete. what if T2 hang up for long time? that is the reason i have to wait only a specific time. (12 seconds).
If T2 does not complete within 12 seconds, I give the User that there s error in T2 process.

Actually this is a reservation program. so user will give message(T1) that booking (T2) is not complete. You can understand I cant keep user untill T2 completes. May be he close the window after some times thinking that booking error. so to avoid that i have to give immediate feed back to him.
James Ward
Ranch Hand

Joined: Apr 27, 2003
Posts: 263
Why is a separate thread(T2) being used to update the database? Why cant the main thread itself update the database.

Typically wait/notify mechanism is used to notify threads; but there better ways now, see this package - java.util.concurrent
Imesh Damith
Greenhorn

Joined: Jul 30, 2009
Posts: 12
that two are parallel process. othewise it takes long time to complete if i put 2 process sequentially.
Charith De Silva
Greenhorn

Joined: Mar 26, 2009
Posts: 18
I feel better approach is to handle this with your application design where you can get updates for every threads you release. Check Observer pattern. Please note you can check thread's validity (thread.isAlive()) only from the control program which releases your threads.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Wait and Sleep methods