Page 1 of 1

About Question enthuware.ocpjp.v7.2.1077 :

Posted: Thu Jan 24, 2013 11:44 am
by ETS User
Hello. Why method below is valid in fourth answer?
status.setIfUnchanged(newstatus, oldstatus);

Re: About Question enthuware.ocpjp.v7.2.1077 :

Posted: Thu Jan 24, 2013 3:29 pm
by admin
Option 4 is not a correct option for the same reason as option 3.

Re: About Question enthuware.ocpjp.v7.2.1077 :

Posted: Thu Jan 24, 2013 4:09 pm
by Guest
It's your remark about option 4
This is valid code but is not thread safe. The value can potentially change after comparison and just befor it is set again. The whole point of using AtomicInteger is to make this operation atomic. compareAndSet is what you need to use.

Re: About Question enthuware.ocpjp.v7.2.1077 :

Posted: Thu Jan 24, 2013 4:55 pm
by admin
Guest wrote:It's your remark about option 4
This is valid code but is not thread safe. The value can potentially change after comparison and just befor it is set again. The whole point of using AtomicInteger is to make this operation atomic. compareAndSet is what you need to use.
This is for option 5 and is why option 5 is invalid as well. The code is valid but it will not achieve what is desired.

Re: About Question enthuware.ocpjp.v7.2.1077 :

Posted: Thu Sep 19, 2013 2:12 am
by The_Nick
I understand why the user got that visual issue, using linux I get the same issue, the explanation of answer number 5 appears to be of answer number 4.. there is an overlapping of answer/question.
But it maybe only on linux. I have seen that on windows the GUI gets visualized slightly different.

Re: About Question enthuware.ocpjp.v7.2.1077 :

Posted: Tue Feb 18, 2020 12:10 pm
by bvrulez
On macOS High Sierra I don't even see the explanation to the fifth option. But I am not complaining since I use the layout which said it might bring issues. :)

Re: About Question enthuware.ocpjp.v7.2.1077 :

Posted: Fri Jun 30, 2023 6:12 am
by steinov
I'm a bit confused about this question.
status.CompareAndSet(oldstatus, newstatus) compares the current value of status with oldstatus, right? Aren't they always equal? Since you set oldstatus to status.get(). Or is the implication that oldstatus may be changed in the /* valid code here */ part?

Re: About Question enthuware.ocpjp.v7.2.1077 :

Posted: Fri Jun 30, 2023 9:15 am
by admin
steinov wrote:
Fri Jun 30, 2023 6:12 am
I'm a bit confused about this question.
status.CompareAndSet(oldstatus, newstatus) compares the current value of status with oldstatus, right?
Well, just to be clear, oldstatus and newstatus are just the names of the variable in the code given in this question. Within AtomicInteger, it is just value. So if you say, "status.CompareAndSet(oldstatus, newstatus) compares the current value contained within the AtomicInteger object referred to by status variable with oldstatus", then you are right. The method checks whether status still has the same old value (oldstatus) that you thought it has when you called get(), (it means no one has changed it after you called get), and if so, then update it with the new value (newstatus).
Aren't they always equal? Since you set oldstatus to status.get(). Or is the implication that oldstatus may be changed in the /* valid code here */ part?
Right but not necessarily in the code within the /* valid code here */ part. While the /* valid code here */ part is executing, there might be some other thread that might execute some other code that changes the value contained in status.

Re: About Question enthuware.ocpjp.v7.2.1077 :

Posted: Sun Jul 02, 2023 12:50 pm
by steinov
Thanks a lot! Your explanation cleared things up for me!

Re: About Question enthuware.ocpjp.v7.2.1077 :

Posted: Thu Dec 14, 2023 10:04 am
by xjavacert
compareAndSet return boolean, not int

Re: About Question enthuware.ocpjp.v7.2.1077 :

Posted: Thu Dec 14, 2023 8:50 pm
by admin
Right. The given code and the correct option do not assume that it returns an int either.