• 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
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

Multithreading: How can total be nearly 5,000 even though loop is up to 100?

 
Ranch Hand
Posts: 434
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Chapter 9, Sierra/Bates, Multithreading, Wait(), Notify() section:



Output:

Waiting for b to complete...
Total is: 4950


How does this happen?
 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

this is adding number from 0 to 99 into total.

total = 0+1+2+3+4+5+..........................99
 
Ranch Hand
Posts: 67
Eclipse IDE Debian Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
1 + 2 + 3 + ... + n = n * (n + 1) / 2

you better start thread b from within the synchronization block.

edit:
I think the proof is something like this:
0 + ... + n = n * (n + 1) / 2

n = 0: 0 = 0 * 2 / 2 = 0

suppose 0 + ... + n = n * (n + 1) / 2 is true

prove: 0 + ... + n + 1 = (n + 1) * (n + 2) / 2

0 + ... + n + 1
= {trivial}
0 + ... + + n + n + 1
= {1 + ... + n = n * (n + 1) / 2}
n * (n + 1) / 2 + n + 1
= {x = y * x / y}
n * (n + 1) / 2 + 2 * (n + 1) / 2
= {x / z + y / z = (x + y) / z}
(n * (n + 1) + 2 * (n + 1)) / 2
= {x * z + y * z = (x + y) * z}
((n + 1) * (n + 2)) / 2
= {trivial}
(n + 1) * (n + 2) / 2
 
author
Posts: 9034
21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Guys,

The wait() and notify() topics are good topics, but they've been removed from the SCJP exam.

hth,

Bert
 
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Bert Bates wrote:Hi Guys,

The wait() and notify() topics are good topics, but they've been removed from the SCJP exam.

hth,

Bert


They are not in SCJP6 but are still in SCJP5 - correct?
 
Sandra Bachan
Ranch Hand
Posts: 434
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

1 + 2 + 3 + ... + n = n * (n + 1) / 2




Wow, I totally forgot about that.

 
Hey, sticks and stones baby. And maybe a wee mention of my stuff:
Free, earth friendly heat - from the CodeRanch trailboss
https://www.kickstarter.com/projects/paulwheaton/free-heat
reply
    Bookmark Topic Watch Topic
  • New Topic