About Question enthuware.ocpjp.v7.2.1077 :
Moderator: admin
About Question enthuware.ocpjp.v7.2.1077 :
Hello. Why method below is valid in fourth answer?
status.setIfUnchanged(newstatus, oldstatus);
status.setIfUnchanged(newstatus, oldstatus);
-
- Site Admin
- Posts: 10103
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1077 :
Option 4 is not a correct option for the same reason as option 3.
If you like our products and services, please help us by posting your review here.
Re: About Question enthuware.ocpjp.v7.2.1077 :
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.
-
- Site Admin
- Posts: 10103
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1077 :
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.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.
If you like our products and services, please help us by posting your review here.
-
- Posts: 132
- Joined: Thu May 16, 2013 9:23 am
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1077 :
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.
But it maybe only on linux. I have seen that on windows the GUI gets visualized slightly different.
-
- Posts: 33
- Joined: Sat Feb 15, 2020 12:44 am
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1077 :
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.
-
- Posts: 19
- Joined: Wed Feb 08, 2023 3:11 am
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1077 :
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?
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?
-
- Site Admin
- Posts: 10103
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1077 :
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).
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.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?
If you like our products and services, please help us by posting your review here.
-
- Posts: 19
- Joined: Wed Feb 08, 2023 3:11 am
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1077 :
Thanks a lot! Your explanation cleared things up for me!
-
- Posts: 1
- Joined: Sat Dec 02, 2023 10:21 pm
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1077 :
compareAndSet return boolean, not int
-
- Site Admin
- Posts: 10103
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1077 :
Right. The given code and the correct option do not assume that it returns an int either.
If you like our products and services, please help us by posting your review here.
Who is online
Users browsing this forum: No registered users and 8 guests