Hello,
This example is from K + B
SCJP Study Guide (
Java 6) pp752-753
I don't understand what this example is trying to demonstrate.
I believe that the following is true about the code snippet below:
(1) There is only one Calculator instance, but there are multiple references to the same Calculator instance.
(2) All three run() methods in the three instances of the Reader class synchronize on the same Calculator object.
(3) The main() method in the Reader class creates three Reader threads, starts all three threads, each
thread enters the synchonized(c) block and invokes wait() on the same Calculator object.
(4) All three Reader threads enter the wait state virtually immediately after being started.
(5) The Calculator thread only runs once.
(6) All three Reader threads print out the same result, one after the other, after they have been notified to enter Runnable.
My question is this: Do I understand or do I not understand how this code snippet works?
It seems overly complex, and seems to only printout the same result three times in succession.
hph@www:~$ javac Reader.java
hph@www:~$ java Reader
Total is: 4950
Total is: 4950
Total is: 4950
hph@www:~$