I have a set of methods say m1,m2,m3,m4. Each one need to wait for previous method to finish. If I call m3, it must wait for m2. I thought of using flag for each method, if finished mark it. but for notifying, if m1 says notify(), I want this notify only for m2, not for other waiting methods.
I have a component A, which has m1,m2,m3 as input methods and m4 as output method. Once m3 happens it will call m4 to produce output. But m1,m2,m3 has to be executed in sequence and can be called from different classes. Waiting and corresponding signal must be there.
How can I go about implementing this?
You don’t want locks, but barriers: m(n + 1) has to wait until m(n) has been called. Below code assume there is only one sequence or method calls (and that m1/2/3 are called from different threads or in proper sequence on the same thread or you’ll wait forever on a latch). If it can happen multiple times, it needs to be reset (or maybe you can get fancy with a Phaser).
However, this is bit weird design, since you want to do it in sequence you should just do all the work in the output method, which waits until all input is in.