Thank you Paul for replying.admin wrote: ↑Wed Jul 11, 2018 9:30 pmThere is a huge difference between the two codes. In the code of this question (2.1438), the variable sorted is always accessed from within a synchronized block, whereas in question 2.1558, the variable threadCounter is not accessed from a synchronized block.
For a thread to be able to see modifications to a variable done by other threads, either the variable must be volatile or the the threads must access the variable from within a synchronized block.
You need to read about "Java Memory Model" (just google it) from a good book. Even oracle's official Java tutorial is a good start. If the book that you are reading doesn't explain this fundamental concept then I will not recommend that book.
HTH,
Paul.
I've googled the concept of "Java Memory Model", but for me that's nothing to do with concurrency, in top of that I'm quite sure those topics about java memory model are not being included in the exam, even in the exam topics at the oracle certified website is not included that concept.
Also before I made the question in this forum I checked the specs about concurrency and syncronized blocks, also the definition of a volatile variable, multiple times and I still couldn't find anything abount this case.
Btw this is my book: learning java. patrick niemeyer daniel leuck 4th edition Java 7