Contended注解了解
JDK8中的Contended注解源码:
/**
* <p>An annotation expressing that objects and/or their fields are
* expected to encounter memory contention, generally in the form of
* "false sharing". This annotation serves as a hint that such objects
* and fields should reside in locations isolated from those of other
* objects or fields. Susceptibility to memory contention is a
* property of the intended usages of objects and fields, not their
* types or qualifiers. The effects of this annotation will nearly
* always add significant space overhead to objects. The use of
* {@code @Contended} is warranted only when the performance impact of
* this time/space tradeoff is intrinsically worthwhile; for example,
* in concurrent contexts in which each instance of the annotated
* class is often accessed by a different thread.
*
* <p>A {@code @Contended} field annotation may optionally include a
* <i>contention group</i> tag. A contention group defines a set of one
* or more fields that collectively must be isolated from all other
* contention groups. The fields in the same contention group may not be
* pairwise isolated. With no contention group tag (or with the default
* empty tag: "") each {@code @Contended} field resides in its own
* <i>distinct</i> and <i>anonymous</i> contention group.
*
* <p>When the annotation is used at the class level, the effect is
* equivalent to grouping all the declared fields not already having the
* {@code @Contended} annotation into the same anonymous group.
* With the class level annotation, implementations may choose different
* isolation techniques, such as isolating the entire object, rather than
* isolating distinct fields. A contention group tag has no meaning
* in a class level {@code @Contended} annotation, and is ignored.
*
* <p>The class level {@code @Contended} annotation is not inherited and has
* no effect on the fields declared in any sub-classes. The effects of all
* {@code @Contended} annotations, however, remain in force for all
* subclass instances, providing isolation of all the defined contention
* groups. Contention group tags are not inherited, and the same tag used
* in a superclass and subclass, represent distinct contention groups.
*
* @since 1.8
*/
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.FIELD, ElementType.TYPE})
public @interface Contended {
/**
* The (optional) contention group tag.
* This tag is only meaningful for field level annotations.
*
* @return contention group tag.
*/
String value() default "";
}
从源码的注释中,我们可以大致得出这样的结论:
使用@Contended来保证被标识的字段或者类不与其他字段出现内存争用。
那么什么是是内存争用?首先我们需要了解CPU是如何从内存中读取数据的。
缓存行

为了提高IO效率,CPU每次从内存读取数据并不是只读取我们需要计算的数据,而是将我们需要的数据周围的64个字节(intel处理器的缓存行是64字节)的数据一次性全部读取到缓存中。这64个字节的数据就称为一个缓存行。
假设现在有两个线程都需要缓存行1(见图)中的数据做运算,假设CPU1需要缓存行1中的第一个字节数据做运算,CPU2需要缓存行1中的第二个字节做运算。此时CPU1和CPU2都需要将缓存行1读取到缓存中,这样就有可能出现缓存不一致现象,为了保证缓存一致性,出现了很多种的缓存一致性协议,其中intel使用了MESI协议来保证缓存一致性。简单的说,当CPU1对缓存行1中的数据做了修改时,会通知CPU2,告诉他数据我修改了,你那边作废了,需要重新从内存读取。反之,CPU2对数据做出修改,CPU1也需要重新读取。这样就会导致大量的IO操作,导致性能降低。
为了避免这种现象,我们需要想办法将这两个数据放到不同的缓存行中,这样就可以避免频繁的读取数据,增加性能。有一种做法是这样的:
public long p1,p2,p3,p4,p5,p6,p7; // cache line padding
private volatile long cursor;
public long p8,p9,p10,p11,p12,p13,p14;// cache line padding
使用额外的字段来对齐缓存行,让cursor字段保证不与其他字段存在同一个缓存行。
Jdk8为我们提供了Contended注解,也是同样的作用。下面我们用两个小程序来测试添加Contended注解和不添加Contended注解的差异。
package com.vertxjava.proxy;
public class ContendedDemo {
public volatile long x;
public volatile long y;
public static void main(String[] args) throws InterruptedException {
ContendedDemo cd = new ContendedDemo();
Thread thread1 = new Thread(() -> {
for (int i = 0; i < 1_0000_0000L; i++) {
cd.x = i;
}
});
Thread thread2 = new Thread(() -> {
for (int i = 0; i < 1_0000_0000L; i++) {
cd.y = i;
}
});
long start = System.currentTimeMillis();
thread1.start();
thread2.start();
thread1.join();
thread2.join();
System.out.println(System.currentTimeMillis() - start);
}
}
我们定义了两个变量x和y,并且使用两个线程对这两个变量做赋值操作。如果不加@Contended注解,x和y有很大概率位于同一个缓存行。就会出现我们刚才所说的频繁的重新从内存读取数据。如果对x变量添加了@Contended注解,则可以保证x与y在不同的缓存行。
注意:如果想要@Contended注解起作用,需要在启动时添加JVM参数:
-XX:-RestrictContended
测试结果
x和y都不增加@Contended注解:
public volatile long x;
public volatile long y;
运行结果:
| 第一次 | 第二次 | 第三次 | 第四次 | 第五次 | 平均 |
|---|---|---|---|---|---|
| 2328ms | 2357ms | 2424ms | 2453ms | 2255ms | 2363ms |
平均耗时:2363毫秒
x添加@Contended注解,y不增加:
@Contended
public volatile long x;
public volatile long y;
运行结果:
| 第一次 | 第二次 | 第三次 | 第四次 | 第五次 | 平均 |
|---|---|---|---|---|---|
| 656ms | 670ms | 664ms | 659ms | 666ms | 663ms |
平均耗时:663毫秒
可以看到,性能差距3倍多。