About Question enthuware.ocpjp.v7.2.1077 :

Help and support on OCA OCP Java Programmer Certification Questions
1Z0-808, 1Z0-809, 1Z0-815, 1Z0-816, 1Z0-817

Moderator: admin

Post Reply
ETS User

About Question enthuware.ocpjp.v7.2.1077 :

Post by ETS User »

Hello. Why method below is valid in fourth answer?
status.setIfUnchanged(newstatus, oldstatus);

admin
Site Admin
Posts: 10066
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post by admin »

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.

Guest

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

Post 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.

admin
Site Admin
Posts: 10066
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post 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.
If you like our products and services, please help us by posting your review here.

The_Nick
Posts: 132
Joined: Thu May 16, 2013 9:23 am
Contact:

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

Post 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.

bvrulez
Posts: 33
Joined: Sat Feb 15, 2020 12:44 am
Contact:

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

Post 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. :)

steinov
Posts: 19
Joined: Wed Feb 08, 2023 3:11 am
Contact:

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

Post 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?

admin
Site Admin
Posts: 10066
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post 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.
If you like our products and services, please help us by posting your review here.

steinov
Posts: 19
Joined: Wed Feb 08, 2023 3:11 am
Contact:

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

Post by steinov »

Thanks a lot! Your explanation cleared things up for me!

xjavacert
Posts: 1
Joined: Sat Dec 02, 2023 10:21 pm
Contact:

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

Post by xjavacert »

compareAndSet return boolean, not int

admin
Site Admin
Posts: 10066
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post by admin »

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.

Post Reply

Who is online

Users browsing this forum: No registered users and 224 guests