1. In the explanation (page 1165) variable sheepCount2 is mentioned, but in the review question(page 909) that variable is called simply s2.
2. I don't understand how the behaviour described in correct answer is guarantied
I found out that this question was in OCP Java 8 Study Guide, and 4 years ago Andrzej Czarny argued that output cannot be determined ahead of time. There is a discussion: https://coderanch.com/t/684463/certification/Sybex-OCP-Java-Study-Guide
, but I don't see that discussion as complete.
Scott Selikoff answered that "Assuming 100 milliseconds is enough time for the tasks submitted to the thread executor to complete
" covers the case, but I think, that there is no time that can guarantee
that main thread will see the changes from executor's thread without proper synchronisation. Main thread may never ever see any changes and may continue reading s2 from it's own cache
an be confident that it's value is 0. Or some other value in between 0 and 100.
Could you please review this question and discussion with Andrzej Czarny once again?