• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Misbehaving variable?

 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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: " + c.total + " 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
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic