• 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

What is this example trying to demonstrate ?

 
Ranch Hand
Posts: 206
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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:~$




 
Bartender
Posts: 1104
10
Netbeans IDE Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Your understanding is correct. This just demonstrates the usage of wait() and notifyAll(). But, this is a very poor and confusing example. The example makes the Calculator also a Thread and tries its maximum to complicate and confuse the reader.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic