Difference between these 2 following methods:

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

Moderator: admin

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

Difference between these 2 following methods:

Post by The_Nick »

Hi,
As in a few questions you specified to go through the AtomicInteger API which are important for the exam, I would like to ask a question about these API.
compareAndSet

public final boolean compareAndSet(int expect,
int update)

Atomically sets the value to the given updated value if the current value == the expected value.

Parameters:
expect - the expected value
update - the new value
Returns:
true if successful. False return indicates that the actual value was not equal to the expected value.

weakCompareAndSet

public final boolean weakCompareAndSet(int expect,
int update)

Atomically sets the value to the given updated value if the current value == the expected value.

May fail spuriously and does not provide ordering guarantees, so is only rarely an appropriate alternative to compareAndSet.

Parameters:
expect - the expected value
update - the new value
Returns:
true if successful.
What's the difference between the two? I can see that in the latter there is an added line:
May fail spuriously and does not provide ordering guarantees, so is only rarely an appropriate alternative to compareAndSet.
What do they actually mean with "may fail spuriously and moreover what kind of ordering are they talking about?

EDIT: Ok in the API they suggest to check this link http://docs.oracle.com/javase/7/docs/ap ... l#Spurious out for an explanation of "spuroiously", however it's not that clear to me.
Is anybody able to express the concept in other words? That link seems to say nothing of concrete...


Thanks in advance.

The_Nick.
Last edited by The_Nick on Fri Jul 19, 2013 7:33 am, edited 1 time in total.

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

Re: Difference between these 2 following methods:

Post by admin »

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: Difference between these 2 following methods:

Post by The_Nick »

So basically only the volatile thing. Ok I still hope there will not be many question about the weakThings.

The_Nick.

Post Reply

Who is online

Users browsing this forum: No registered users and 224 guests