Adding @atomic operations to Java


How might atomic operations work in Java, and is there a current alternative in OpenJDK/Hotspot it could translate to.


In my previous article on Making operations on volatile fields atomic. it was pointed out a few times that "fixing" previous behaviour is unlikely to go ahead regardless of good intentions.

An alternative to this is to add an @atomic annotation.  This has the advantage of only applying to new code and not risk breaking old code.

Note: The use of a lower case name is intentional as it *doesn't* follow current coding conventions.

Atomic operations

Any field listed with an @atomic would make the whole expression atomic.  Variables which are non-volatile and non-atomic could be read at the start, or set after the completion of the expression.  The expression itself may require locking on some platforms, CAS operations or TSX depending on the CPU technology.

If fields are only read, or only one is written too, this would be the same as volatile.

Atomic Boolean

Currently the AtomicBoolean uses 4 bytes, plus an object header, with possible padding (as well as a reference)  If the field was inlined it could look like this

@atomic boolean flag;
// toggle the flag.
this.flag = !this.flag;

But how would it work?  Not all platforms support 1 byte atomic operations e.g. Unsafe does have a 1 byte CAS operations.  This can be done with masking.

// possible replacement.
while(true) {
     int num = Unsafe.getUnsafe().getVolatileInt(this, FLAG_OFFSET & ~3); // word align the access.
     int value ^= 1 << ~(0xFF << (FLAG_OFFSET & 3) * 8) ;
     if (Unsafe.getUnsafe().compareAndSwapInt(this, FLAG_OFFSET & ~3, num, value))

Atomic Double

A type which is not supported is AtomicDouble, but this is a variation on AtomicLong.  Consider this example.

@atomic double a = 1;
volatile double b = 2;

a += b;

How might it be implemented today?

while(true) {
    double _b = Unsafe.getUnsafe().getVolatileDouble(this, B_OFFSET);
    double _a = Unsafe.getUnsafe().getVolatileDouble(this, A_OFFSET);
    long aAsLong = Double.doubleToRawLongBits(_a);
    double _sum = _a + _b;
    long sumAsLong = Double.doubleToRawLongBits(_a);
    if (Unsafe.getUnsafe().compareAndSwapLong(this, A_OFFSET, aAsLong, sumAsLong))

Two Atomic Fields

Using Intel TSX, you can wrap a hardware transaction around a number of fields, but what if you don't have TSX, could it still be done, without resorting to a lock.

@atomic int a = 1, b = 2;

a += b * (b % 2 == 0 ? 2 : 1);

This can still be done with the CAS if the fields are together.  There is a CAS2 operation planned to be able to check two 64-bit values.  For now, this example will use two 4-byte values.

assert A_OFFSET + 4 == B_OFFSET;
while(true) {
    long _ab = Unsafe.getUnsafe().getVolatileLong(this, A_OFFSET);
    int _a = getLowerInt(_ab);
    int _b = getHigherInt(_ab);
    int _sum = _a + _b * (_b % 2 == 0 ? 2 : 1);
    int _sum_ab = setLowerIntFor(_ab, _sum);
    if (Unsafe.getUnsafe().compareAndSwapLong(this, A_OFFSET, _ab, _sum_ab))

Note: This operation can handle the change of either a, or b or both in an atomic way.


A common use case operations on immutable objects such as BigDecimal.

@atomic BigDecimal a;
BigDecimal b;

a = a.add(b);

could be implemented this way on systems with CompressedOops or 32-bit JVMs.

BigDecimal _b = this.b;
while(true) {
    BigDecimal _a = (BigDecimal) Unsafe.getUnsafe().getVolatileObject(this, A_OFFSET);
    BigDecimal _sum = _a.add(_b);
    if (Unsafe.getUnsafe().compareAndSwapLong(this, A_OFFSET, _a, _sum))

More complex examples

There will always be examples which are too complex for your platform.  They might be fine on a System with TSX or HotSpot supported systems, however you need a fall back.

@atomic long a, b, c, d;

a = (b = (c = d + 4) +  5 ) + 6;

This is not support currently as it set multiple long values in one expression.  However, a fall back could be to use the existing lock.

synchronized(this) {
    a = (b = (c = d + 4) +  5 ) + 6;


By adding an annotation, we could add atomic operations to regular fields, without the need to change the syntax.  This would be a natural extension to the language without breaking backward comparability


  1. Hi Peter,

    I know I'm probably arguing a point of taste, but I think I prefer the proposal in JEP-193. While it is more verbose and not as easy to use, it does make the behaviour obvious at the call site, rather than at the type declaration. When building concurrent code I find that it makes the algorithm easier to reason about when the code, when I can distinguish where the fences are being issued through an alternative name or syntax from a typical non-atomic operation. Having to refer back to the type declaration for each variable increases the cognitive load required to understand the code.

    I think for these "low level" atomic operations language design focus on clarity rather than ease of use.


  2. I guess that letting @atomic boolean eat 4 bytes would be no big deal (you'd usually pad it by 64 bytes from both sides anyway).

    Silently using `synchronized(this)` is dangerous as it may cause deadlocks.

    1. You can implement atomic operations on a byte by checking the whole 4-bytes around it. See my previous article. synchronized does have a danger of deadlocks, the aim is have a generic fall back, there are alternatives such as an extra lock added to the end of the object. Where there is multiple potential locks, the code could ensure they are locked in order.


Post a Comment

Popular posts from this blog

Java is Very Fast, If You Don’t Create Many Objects

System wide unique nanosecond timestamps

Comparing Approaches to Durability in Low Latency Messaging Queues