[concurrency-interest] AtomicReferenceFieldUpdater vs Unsafe

Martin Buchholz martinrb at google.com
Wed Nov 16 18:44:58 EST 2011


On Wed, Nov 16, 2011 at 13:37, Dr Heinz M. Kabutz
<heinz at javaspecialists.eu>wrote:

> In Java 6, classes like ConcurrentLinkedQueue and SynchronousQueue used
> the AtomicReferenceFieldUpdater to update the next, head, etc. fields.
>
> In Java 7, this was changed to instead use Unsafe.compareAndSwapObject()
> directly.
>
> The AtomicReferenceFieldUpdater does a bunch of error checking every time
> it is called, like this:
>
>           if (obj == null || obj.getClass() != tclass || cclass != null ||
>               (update != null && vclass != null &&
>                vclass != update.getClass()))
>               updateCheck(obj, update);
>           return unsafe.compareAndSwapObject(**obj, offset, expect,
> update);
>
> My thinking is that the programmers changing ConcurrentLinkedQueue et al
> probably wanted to improve the performance by not having to do all that
> checking every time it is called.  The Unsafe.compareAndSwapObject() method
> is probably compiled to a single CPU instruction.
>
> Is that correct?
>
> Yes.


> Is there any other reason for this change?
>
> The previous way was more principled, in the manner of "eat your own
dogfood".  Maybe we've become just a tiny bit less principled.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://cs.oswego.edu/pipermail/concurrency-interest/attachments/20111116/59defd56/attachment.html>


More information about the Concurrency-interest mailing list