• 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
  • Tim Cooke
  • paul wheaton
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

Threads and Sync

 
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A taked this code from K&B book, WHY IT THROWS Total is: 4950


[ June 20, 2008: Message edited by: Campbell Ritchie ]
 
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Ranch Hand
Posts: 156
Android Eclipse IDE
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic