A simple wait() notify() would have to be modified with some signals to make sure the proper order is conserved. Here is an example:
Other options would be to have C lock on a different object then A. B initially locks on the same one A is using and wait()s. A notify()s B, B does its work, then synchronizes on the same Object as C and notify()s it. This can be a bit tricky to implement though, you have to be sure all your data you are protecting are properly protected with the double locks, and that you don't get into a deadlock situation.
However, if your target app is at Java 1.5 or later, you should probably take advantage of the java.util.concurrent.Lock/Condition classes. Look up the tutorial for Concurrency in Java and you will see example usage.
/*** Edit ***/
The notify()s should be notifyAll()s, changed in above code
There is no way to determine if B will run after A has send notify().Remember notify() invokes a thread from the wait set and then that thread competes for acquisition of lock on an object.Only the Operating system determines which thread will take the lock.Also note that if there are 2 threads X and Y where X has maximum priority and Y has minimum priority and both are in ready to run state...in such a scenario it only increases the chances of X in running state over Y but does not guarantee execution of X before Y.
I was thinking about Priority Setting of each thread.But i am not sure it will work or not .
It is generally wise to resist the temptation to tweak thread priorities. As soon as you start modifiying priorities, the behavior of your application becomes platform-specific and you introduce the risk of starvation.
posted 11 years ago
Just made some correction based on Steve Luke code. Basically the notifiier will triggers the first thread to run and will wait until all get executed
I hope this correction reinforce the order for thread execution.
posted 11 years ago
Thanks all for you kind help specially Alan and Luke.
SCJP1.4<br />SCWCD1.5<br />"Nothing is impossible"