Hello there
The following code comes straight out of the Sierra & Bates java6 study guide p752 to which I have added a few SOP statements & reduced the loop length from 100 to 10 for readability.
This produces the following output:
Thread Thread-1 Waiting for calculation...
Thread Thread-2 Waiting for calculation...
Thread Thread-3 Waiting for calculation...
Thread Thread-0 Entering Calculator.run()
0-1-2-3-4-5-6-7-8-9Total is: 45 count= 10
Total is: 45 count= 10
Total is: 45 count= 10
With the for loop set to 100 (as per the book) it consistently produces:
Total is: 4950
with the for loop set to 10 it consistently produces:
Total is: 45
Then on a few runs it produced an output similar to this:
0-1-3- ... -22-45Total is: 45 count= 10
You will notice that the final value of the "control" variable count is always correct.
Finally replacing notifyAll() by notify() makes NO difference whatsoever to the output as all 3 threads terminate.
Please help, I am lost.