This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Misbehaving variable? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Misbehaving variable?" Watch "Misbehaving variable?" New topic

Misbehaving variable?

oli ikwayo

Joined: Jun 15, 2011
Posts: 11
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
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.
Yuan Zhang

Joined: Sep 02, 2011
Posts: 1
Because Calculator is a subclass of Thread, and you only created one thread for Calculator instance, this thread will be dead after running once.
So the calculation only ran once, and value of count and total didn't change after that.
oli ikwayo

Joined: Jun 15, 2011
Posts: 11
Hello Yuan,
I am not too sure I understand what you meant.

I disagree with you about the Calculator thread. Although that thread (of execution) will die after issuing notifyAll(), the Thread object itself is still a valid object, ie it is not garbage collected as there are still 2 references accessing it when the 3 Reader threads are issuing:
System.out.println("Total is: " + + " count= " + c.count);
if the Calculator object had been garbage collected by that time, I guess the code would not compile as it would be referring to variables that no longer exist.

Thus the question remains: Why is total behaving differently to count?
oli ikwayo

Joined: Jun 15, 2011
Posts: 11
I apologize everybody, (I must have been possessed when I wrote that post).
There is nothing strange happening here.
I misread the original code & I did not pay any attention to the one I wrote.
The variable total is correctly adding up the 10 values of the loop variable i, which sums up to 45, and the count variable increments from 0 to 10.
I agree. Here's the link:
subject: Misbehaving variable?
Similar Threads
Difference between notify and notifyAll
wait(), notify, notifyAll() doubt
Question on Thread example in Kathy Sierra 1.4 book
Code using notifyAll() runs indefinitely
Thread interaction