Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

False Sharing & Java 7

DZone's Guide to

False Sharing & Java 7

· Java Zone
Free Resource

Just released, a free O’Reilly book on Reactive Microsystems: The Evolution of Microservices at Scale. Brought to you in partnership with Lightbend.

In my previous post on False Sharing I suggested it can be avoided by padding the cache line with unused long fields.  It seems Java 7 got clever and eliminated the unused fields, thus re-introducing false sharing.  Below is some code that should give a more robust means of avoiding false sharing when using volatile counters

import java.util.concurrent.atomic.AtomicLongArray;

public final class FalseSharing
    implements Runnable
{
    public final static int THREADS = 4;
    public final static long ITERATIONS = 500L * 1000L * 1000L;
    private final int arrayIndex;

    private static AtomicLongArray[] longs = new AtomicLongArray[THREADS];

    static
    {
        for (int i = 0; i < longs.length; i++)
        {
            longs[i] = new AtomicLongArray(5); // < 5 == false sharing
        }
    }

    public FalseSharing(final int arrayIndex)
    {
        this.arrayIndex = arrayIndex;
    }

    public static void main(final String[] args) throws Exception
    {
        final long start = System.nanoTime();
        runTest();
        System.out.println("duration = " + (System.nanoTime() - start));
    }

    private static void runTest() throws InterruptedException
    {
        Thread[] threads = new Thread[THREADS];

        for (int i = 0; i < threads.length; i++)
        {
            threads[i] = new Thread(new FalseSharing(i));
        }

        for (Thread t : threads)
        {
            t.start();
        }

        for (Thread t : threads)
        {
            t.join();
        }
    }

    public void run()
    {
        long i = ITERATIONS + 1;
        while (0 != --i)
        {
            longs[arrayIndex].set(0, i);
        }
    }
}

 

The compiler would find it very difficult to eliminate the array and thus re-introduce false sharing.  Hopefully this is not "famous last words"...!  It is a shame Java does not allow the individual elements of an array to be marked volatile but we have a useful class in java.util.concurrent.atomic that gives us what we need.  With this code I get similar performance results to those stated in the previous False Sharing article.

 

From http://mechanical-sympathy.blogspot.com/2011/08/false-sharing-java-7.html

Strategies and techniques for building scalable and resilient microservices to refactor a monolithic application step-by-step, a free O'Reilly book. Brought to you in partnership with Lightbend.

Topics:

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}