package geym.conc.ch5.disruptor;


/**
 * remove --release and add this command to javac
 * --add-exports java.base/jdk.internal.vm.annotation=ALL-UNNAMED
 */

import jdk.internal.vm.annotation.Contended;

public final class FalseSharing implements Runnable {
    public final static int NUM_THREADS = 2; // change
    public final static long ITERATIONS = 500L * 1000L * 1000L;
    private final int arrayIndex;

    private static VolatileLong[] longs = new VolatileLong[NUM_THREADS];
    static {

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

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

    public static void main(final String[] args) throws Exception {
        final long start = System.currentTimeMillis();
        // 这里就是访问一下，防止不使用的变量被系统优化删除
        VolatileLong v = longs[0];
//        System.out.println(v.p1+v.p2+v.p3+v.p4+v.p5+v.p6+v.p7);
        runTest();
        System.out.println("duration = " + (System.currentTimeMillis() - start));
    }

    private static void runTest() throws InterruptedException {
        Thread[] threads = new Thread[NUM_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].value = i;
        }
    }

    //JDK 7 某些版本 和 JDK 8中 会把不用的数据优化 导致 这种优化手段失效
    // Unlock: -XX:-RestrictContended (JDK 8  option)
//    @jdk.internal.vm.annotation.Contended
    public final static class VolatileLong {
        public volatile long value = 0L;
//        public long p1, p2, p3, p4, p5, p6,p7; // comment out
    }
}
