aspose file tools*
The moose likes Beginning Java and the fly likes Threads and Sync Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Threads and Sync" Watch "Threads and Sync" New topic
Author

Threads and Sync

podonga poron
Ranch Hand

Joined: May 12, 2008
Posts: 55
A taked this code from K&B book, WHY IT THROWS Total is: 4950


[ June 20, 2008: Message edited by: Campbell Ritchie ]
Keith Nagle
Ranch Hand

Joined: May 06, 2008
Posts: 65
Because, for each int i,
your adding it to total.
Total is given its default value for type int
(because its an instance variable remember).
So, i is added to this each time.
What is (0 + 1 + 2 + 3 + 4 + ... + 99)?
=> 4950!
best regards.
K

P.S be careful with your wording.
It doesn't throw total. It prints it!
[ June 20, 2008: Message edited by: Keith Nagle ]

SCJP 5.0
Priety Sharma
Ranch Hand

Joined: Jun 10, 2008
Posts: 156

Hi,

Good use of wait and notify to let the ThreadB execute before ThreadA completes.

But you could have done the same without using wait() and notify() and just replacing b.wait() with b.join().


Also the topic for the question could have been "Loop output" instead of "Threads and Sync".

Just an observation.

Regards,
Priety.


Priety.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Threads and Sync