Page 1 of 1

Difference between these 2 following methods:

Posted: Fri Jul 19, 2013 7:28 am
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.

Re: Difference between these 2 following methods:

Posted: Fri Jul 19, 2013 7:32 am
by admin

Re: Difference between these 2 following methods:

Posted: Fri Jul 19, 2013 7:37 am
by The_Nick
So basically only the volatile thing. Ok I still hope there will not be many question about the weakThings.

The_Nick.