Page 1 of 1
About Question enthuware.ocpjp.v7.2.1352 :
Posted: Thu Oct 09, 2014 4:13 pm
by shcher
It seems to me that the option 4 is the correct answer due to possible memory consistency problems, though I was unable to reproduce memory consistency failure. "Java Concurrency in Practice" suggests defining such shared variables as "volatile".
Also see Double-checked Singleton and how it was broken in the earlier versions of Java:
http://javarevisited.blogspot.com/2014/ ... -java.html
Does anybody know why/why not memory consistency problems matter here?
Re: About Question enthuware.ocpjp.v7.2.1352 :
Posted: Thu Oct 09, 2014 7:34 pm
by admin
The two cases are different. You need volatile if you are reading a variable directly without using a synchronized block. Although volatile by itself will not prevent race condition, volatile will ensure that an updated value will be visible at some point in future.
In this question, all the access is happening from within synchronized blocks, so volatile is not required. But of course if you try to access x and y from outside a synchronized block, then you may not see updated values of x and y at all.
HTH,
Paul.
Re: About Question enthuware.ocpjp.v7.2.1352 :
Posted: Sun Sep 13, 2015 3:05 am
by nsobchuk
Did I get it correct, that synchronized(this) locks the tc object and not the thread instance ( new Thread(tc) ) ?
Re: About Question enthuware.ocpjp.v7.2.1352 :
Posted: Sun Sep 13, 2015 7:54 am
by admin
Yes, that is correct. When you do synchronized(xyz), you acquire the lock of the object pointed to by xyz. If you are not sure what is xyz pointing to, you can always do System.out.println(xyz) and confirm

Re: About Question enthuware.ocpjp.v7.2.1352 :
Posted: Wed Sep 23, 2015 12:43 pm
by lawdog
I argue that answer B) is incorrect because we cannot be certain that the first thread's modification to x and y will be visible to the second thread. I propose that a possible output could be:
"x = 1, y = 1"
"x = 1, y = 1" //This might be the case if the first thread to execute loops only once before the Thread manager decides to give time to the second thread.
This means that B) "It will keep printing values which show x and y always as equal and increasing by 1 at each line" is false due to violating "increasing by 1 at each line".
Re: About Question enthuware.ocpjp.v7.2.1352 :
Posted: Wed Sep 23, 2015 6:17 pm
by admin
lawdog wrote:I argue that answer B) is incorrect because we cannot be certain that the first thread's modification to x and y will be visible to the second thread.
Why do you think we cannot be certain that the first thread's modification to x and y will be visible to the second thread?
Re: About Question enthuware.ocpjp.v7.2.1352 :
Posted: Thu Sep 24, 2015 9:07 am
by lawdog
I must have confused myself. As a read more about the memory model I realize that it is guaranteed that entering or leaving a synchronized block causes the updates to be available.
Re: About Question enthuware.ocpjp.v7.2.1352 :
Posted: Mon Feb 01, 2016 5:35 pm
by krohani
I also initially thought that one thread's modification of x and y
may not be visible to the other thread, however upon further research I found this explanation on why that will never be the case. I think Paul also is saying the same thing in more simpler terms.
...synchronized keyword involves locking and unlocking. Before entering into synchronized method or block thread needs to acquire the lock, at this point it
reads data from main memory rather than cache and when it release the lock,
it flushes write operation into main memory which eliminates memory inconsistency errors.
Read more:
http://javarevisited.blogspot.com/2011/ ... z3yxTsPJLS
Re: About Question enthuware.ocpjp.v7.2.1352 :
Posted: Wed Jan 04, 2017 1:45 pm
by jagoneye
The people who are confused about synchronization shows that you have not read the low level Locks properly. First read how those are used to lock and unlock then synchronized use will be clear as crystal.
