JDK中注解@Contended是干什么的?怎么使用?

问题如图:
JDK中注解@Contended是干什么的?怎么使用? 这个注解还是在jdk1.8中加入的呢。。

附上代码:
图片描述

回答:

避免伪共享(false sharing)。
先引用个伪共享的解释:

缓存系统中是以缓存行(cache line)为单位存储的。缓存行是2的整数幂个连续字节,一般为32-256个字节。最常见的缓存行大小是64个字节。当多线程修改互相独立的变量时,如果这些变量共享同一个缓存行,就会无意中影响彼此的性能,这就是伪共享。

然后给一段参考代码:

public class FalseSharing implements Runnable {

public final static int NUM_THREADS = 4; // change

public final static long ITERATIONS = 500L * 1000L * 1000L;

private final int arrayIndex;

private static VolatileLong3[] longs = new VolatileLong3[NUM_THREADS];

static {

for (int i = 0; i < longs.length; i++) {

longs[i] = new VolatileLong3();

}

}

public FalseSharing(final int arrayIndex) {

this.arrayIndex = arrayIndex;

}

public static void main(final String[] args) throws Exception {

long start = System.nanoTime();

runTest();

System.out.println("duration = " + (System.nanoTime() - 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;

}

}

public final static class VolatileLong {

public volatile long value = 0L;

}

// long padding避免false sharing

// 按理说jdk7以后long padding应该被优化掉了,但是从测试结果看padding仍然起作用

public final static class VolatileLong2 {

volatile long p0, p1, p2, p3, p4, p5, p6;

public volatile long value = 0L;

volatile long q0, q1, q2, q3, q4, q5, q6;

}

// jdk8新特性,Contended注解避免false sharing

// Restricted on user classpath

// Unlock: -XX:-RestrictContended

@sun.misc.Contended

public final static class VolatileLong3 {

public volatile long value = 0L;

}

}

本地机器上的测试结果:

VolatileLong: duration = 29594765000

VolatileLong2:duration = 7234555000

VolatileLong3:duration = 7167475000

加一句
要注意的是user classpath使用此注解默认是无效的,需要在jvm启动时设置-XX:-RestrictContended

以上是 JDK中注解@Contended是干什么的?怎么使用? 的全部内容, 来源链接: utcz.com/p/178410.html

回到顶部