false sharing 伪共享

198 阅读1分钟
package com.stan.algo.algo;

/**
 * 伪共享
 * long 8 byte  
 */

/**
 * @ClassName: FalseSharing
 * @Description:
 * @Date: 2021/04/29 下午 5:13
 * @Version: 1.0
 **/
public class FalseSharing implements Runnable {
    public final static long ITERATIONS = 500L * 1000L * 100L;
    private int arrayIndex = 0;
    private static ValueNoPadding[] longs;

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

    public static void main(final String[] args) throws Exception {
        for (int i = 1; i < 10; i++) {
            System.gc();
            final long start = System.currentTimeMillis();
            runTest(i);
            System.out.println("Thread num " + i + " duration = " + (System.currentTimeMillis() - start));
        }
    }

    private static void runTest(int NUM_THREADS) throws InterruptedException {

        Thread[] threads = new Thread[NUM_THREADS];

        longs = new ValueNoPadding[NUM_THREADS];

        for (int i = 0; i < longs.length; i++) {
            longs[i] = new ValueNoPadding();
        }

        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 final static class ValuePadding {
        //  构造 64字节 一个缓存行 cache line
        protected long p1, p2, p3, p4, p5, p6, p7;
        protected volatile long value = 0L;
        protected long p9, p10, p11, p12, p13, p14;
        protected long p15;
    }

    @Override
    public void run() {
        long i = ITERATIONS + 1;
        while (0 != --i) {
            longs[arrayIndex].value = 0L;
        }
    }

    public final static class ValueNoPadding {
        // protected long p1, p2, p3, p4, p5, p6, p7;
        protected volatile long value = 0L;
        // protected long p9, p10, p11, p12, p13, p14, p15;
    }
}

效率对比

image.png image.png