Okay, I don't think I like that approach. In your scenario, one update from the server tells the monitor to make two events -> one tells the plot things have changed and one tells the instrument it has changed. Then the monitor triggers several events in the Plot class to get the value from the various instruments so it can determine what changed.
Let's say the server tells the monitor Motor # 1 has changed. Then this is the flow of your program:
All so that Plot can get a single change to Motor 1. The way I see it, the motors and the water sensors are models, and the plot is the view. We can take a page out of the swing book, and make the model tell the view when it changed, instead of making the view ask when the model changed. In this case, the Plot is registered with the Motors and the Sensors. The Monitor tells the Motor it changes, and the Motor tells the Plot it changed. It would look like this:
One event from the server yields one event from the monitor which yields one event from the particular instrument which changed. Now, how Plot gets the values of other instruments is unimportant, it might query them, or it might hold a local cache, or maybe it doesn't need to do anything...
But specifically to your need for checking 'isValid', this really only needs to happen if you don't have a good 'default' value. If the value hasn't been set yet, provide some default value (a good default value would be one that is obvious it is pre-reading. For numbers this is usually 0 in
Java, but sometimes people use -1.)
The code for Plot might be something like this:
A Motor might look like this:
And whatever code makes these things might look like:
Now, this really has gone a long way away from what your real problem actually is. Your real problem seems to come from this:
Arun Bommannavar wrote:Sometimes, when Plot class tries to read Motor value and the code gets stuck in "while(!valid)" loop because first time value hasnt been read yet. Even when the value is read and it sets valid = true, the code is still stuck in "while(!valid)" loop. This I found out by print statements. I tried declaring valid as volatile but it doesnt help. In the following code, I changed wait() to wait(20) and then printed the value for valid, it still printed false. So I am not sure why it gets stuck in the while(!valid) loop.
My feeling for this is:
1) You are using an older version of the JVM (pre-Java 1.5) in which volatile was broken. So the value of valid may never reach from one
thread to the other. Though this seems doubtful since you are synchronizing which should publish the value anyway
2) You have different instances of the object. You are calling setValid() on one instance and checking isValid on another. The one whose valid flag is being checked never changes because you never set it. For example, you have 3 motors and 3 sensors. Motor 1 gets updated. Plot checks motor 1, gets the value because its valid flag was set. It then checks Motor 2, but has to wait, because Motor 2 has not been updated. And when Motor 2
never gets updated, then Plot
never gets out of the getVal() method.
Without a compilable example I can't be sure though.