This week's book giveaway is in the Agile and Other Processes forum.
We're giving away four copies of Darcy DeClute's Scrum Master Certification Guide: The Definitive Resource for Passing the CSM and PSM Exams and have Darcy DeClute on-line!
See this thread for details.
  • 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
  • Devaka Cooray
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Jeanne Boyarsky
  • Tim Cooke
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Mikalai Zaikin
  • Carey Brown
Bartenders:

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: 9050
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.

 
It runs on an internal combustion engine. This ad does not:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic