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

Download Microservices for Java Developers: A hands-on introduction to frameworks and containers. Brought to you in partnership with Red Hat.

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

Download Modern Java EE Design Patterns: Building Scalable Architecture for Sustainable Enterprise Development.  Brought to you in partnership with Red Hat

Topics:

Opinions expressed by DZone contributors are their own.

THE DZONE NEWSLETTER

Dev Resources & Solutions Straight to Your Inbox

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

X

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

{{ parent.tldr }}

{{ parent.urlSource.name }}