• 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
  • Liutauras Vilda
  • Tim Cooke
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Devaka Cooray
  • Ron McLeod
  • paul wheaton
Saloon Keepers:
  • Tim Moores
  • Piet Souris
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Frits Walraven
  • Scott Selikoff

For Jeanne and Scott

 
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Firstly, congrats on your new book.

But I need to ask a question from your Java 8 certification book.

It relates Chapter 7 Q17

If one thread is incrementing sheepCount2 and another thread is printing its value, how can the answer be B ? Surely the answer must be C ?

That said, every time I've executed the code it always print s 100 100.

Can you explain ?

Thanks,

Michael
 
Marshal
Posts: 76830
366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please supply fuller details of the question for those of us who don't have the book.
 
author & internet detective
Posts: 41405
853
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Michael,
It so happens that the output is the same. But it's not guaranteed to be. This is something you have to watch out for with concurrency (and streams and binarySearch). When is the answer guaranteed to be true vs the JDK we are using was implemented to use that result. We highlight this distinction in the book for the APIs where you need to be aware
 
I was her plaything! And so was this tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic