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.
Can T2 inform T1 that it is done with updating database, so that there is no need for T1 to poll the db?
Joined: Jul 30, 2009
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.
Joined: Apr 27, 2003
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
Joined: Jul 30, 2009
that two are parallel process. othewise it takes long time to complete if i put 2 process sequentially.
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.