I have been trying to understand the threads chapter (CH 9) in the
Java 2
SCJP Study Guide written by Bert Bates, and Kathy Sierra. This question is very specific and related to page 531. I will attempt to give a synopsis of the sceanrio which the authors are attempting to explain, and then state either the problem with the code example they provide, or demonstrate my misunderstanding of the principles they are trying to teach. Since I am pretty new to threads, and I consider it very important for the exam, and in real life, I would like to know what the correct answer is!!!
Notify and Wait (
Thread Interaction) Scenario
A user interfaces with an Operator object (Thread). The Operator class interfaces with a Machine object (Thread), which in turn interfaces with the actual Hardware. The user inputs a new shape for the Machine to cut, and the operator object gets this information from the user. The Machine object actually instantiates the operator object (Thread) and then waits until the operator object has completed calculating the Machine steps necessary to send to the Hardware based on the user input. Once this has been completed a notify signal is sent, and the Machine object then sends the Machine steps calculated by the Operator object to the specific Hardware. A common object used by both the Hardware object, and the Operator object, stores the actual Machine cutting steps.
Here is the applicable code and some (but not all) text as written by the authors: (Note: They only included the Operator and Machine class code):
" It is important that the user thread does not modify the machine steps while the hardware thread is using them, so this reading and writing should be synchronized. The resulting code would like this:
class Operator extends Thread {
public void run() {
while (true) {
// Get shape from user
synchronized (this) {
// Calculate new machine steps from shape
notify();
}
}
}
}
class Machine extends Thread {
Operator operator; //assume this gets initialized
public void run() {
while(true) {
synchronized(operator) {
try {
operator.wait();
} catch (InterruptedException ie) {}
// Send machine steps to hardware
}
}
}
}
.......While one shape is being processed by the hardware, the user may interact with the system and specify another shape to be cut. When the user is finished with the shape and it is time to cut it, the operator thread attempts to enter the synchronized block, maybe blocking until the machine thread has finished with the previous machine steps. When the machine thread has finished, it repeats the loop, going again to the waiting state (and therefore releasing the lock). Only then can the oprator thread enter the synchronized block and overwrite the machine steps with the new ones. "
PROBLEM (as I see it): After startup, and assuming the Operator thread is blocking until the Machine thread finishes, the code does not match the authors description of what happens after the Machine thread finishes sending steps to the hardware. For example, as currently written, the Machine thread would block while attempting to aquire a lock on the operator object (not wait)after finishing the previous machine steps, while the operator thread would then be calculating new machine steps. After the operator thread completed those steps, it would send a notify signal, but since the machine thread had not yet called wait at this point, this notify signal (and a whole set of machine instructions)is wasted (I think?). After the operator thread exited the synchronized block, the machine thread would acquire the operator objects lock and then wait until more user input and futher machine steps were calculated by the operator thread and another notify signal was sent.
CONCLUSION:
I am guesing a little bit, but their description makes a whole lot more sense (and it also seems to me to be better code anyway) if the while(true) and synchronized(operator) lines are swapped. I am pretty sure this is what was intended, but since I am fairly new to threads, I would like to make sure I understand this section thoroughly. For example, if I am wrong, in my opinion, I don't understand enough about the notify () method, etc.
Please let me know whether my analysis and conclusions are correct, or if I am out in left field. If I am out there, any additional information concerning the notify method and how it works would be appreciated.
Thanks.
Rick