• 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 all forums
this forum made possible by our volunteer staff, including ...
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

What is this example trying to demonstrate ?

Ranch Hand
Posts: 206
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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

Posts: 1104
Netbeans IDE Java Ubuntu
  • Mark post as helpful
  • send pies
  • 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.
Opportunity is missed by most people because it is dressed in overalls and looks like work - Edison. Tiny ad:
the value of filler advertising in 2020
    Bookmark Topic Watch Topic
  • New Topic