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 "";
}
從源碼的注釋中,我們可以大致得出這樣的結(jié)論:
使用@Contended來(lái)保證被標(biāo)識(shí)的字段或者類不與其他字段出現(xiàn)內(nèi)存爭(zhēng)用。
那么什么是是內(nèi)存爭(zhēng)用?首先我們需要了解CPU是如何從內(nèi)存中讀取數(shù)據(jù)的。
緩存行

為了提高IO效率,CPU每次從內(nèi)存讀取數(shù)據(jù)并不是只讀取我們需要計(jì)算的數(shù)據(jù),而是將我們需要的數(shù)據(jù)周圍的64個(gè)字節(jié)(intel處理器的緩存行是64字節(jié))的數(shù)據(jù)一次性全部讀取到緩存中。這64個(gè)字節(jié)的數(shù)據(jù)就稱為一個(gè)緩存行。
假設(shè)現(xiàn)在有兩個(gè)線程都需要緩存行1(見(jiàn)圖)中的數(shù)據(jù)做運(yùn)算,假設(shè)CPU1需要緩存行1中的第一個(gè)字節(jié)數(shù)據(jù)做運(yùn)算,CPU2需要緩存行1中的第二個(gè)字節(jié)做運(yùn)算。此時(shí)CPU1和CPU2都需要將緩存行1讀取到緩存中,這樣就有可能出現(xiàn)緩存不一致現(xiàn)象,為了保證緩存一致性,出現(xiàn)了很多種的緩存一致性協(xié)議,其中intel使用了MESI協(xié)議來(lái)保證緩存一致性。簡(jiǎn)單的說(shuō),當(dāng)CPU1對(duì)緩存行1中的數(shù)據(jù)做了修改時(shí),會(huì)通知CPU2,告訴他數(shù)據(jù)我修改了,你那邊作廢了,需要重新從內(nèi)存讀取。反之,CPU2對(duì)數(shù)據(jù)做出修改,CPU1也需要重新讀取。這樣就會(huì)導(dǎo)致大量的IO操作,導(dǎo)致性能降低。
為了避免這種現(xiàn)象,我們需要想辦法將這兩個(gè)數(shù)據(jù)放到不同的緩存行中,這樣就可以避免頻繁的讀取數(shù)據(jù),增加性能。有一種做法是這樣的:
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
使用額外的字段來(lái)對(duì)齊緩存行,讓cursor字段保證不與其他字段存在同一個(gè)緩存行。
Jdk8為我們提供了Contended注解,也是同樣的作用。下面我們用兩個(gè)小程序來(lái)測(cè)試添加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);
}
}
我們定義了兩個(gè)變量x和y,并且使用兩個(gè)線程對(duì)這兩個(gè)變量做賦值操作。如果不加@Contended注解,x和y有很大概率位于同一個(gè)緩存行。就會(huì)出現(xiàn)我們剛才所說(shuō)的頻繁的重新從內(nèi)存讀取數(shù)據(jù)。如果對(duì)x變量添加了@Contended注解,則可以保證x與y在不同的緩存行。
注意:如果想要@Contended注解起作用,需要在啟動(dòng)時(shí)添加JVM參數(shù):
-XX:-RestrictContended
測(cè)試結(jié)果
x和y都不增加@Contended注解:
public volatile long x;
public volatile long y;
運(yùn)行結(jié)果:
| 第一次 | 第二次 | 第三次 | 第四次 | 第五次 | 平均 |
|---|---|---|---|---|---|
| 2328ms | 2357ms | 2424ms | 2453ms | 2255ms | 2363ms |
平均耗時(shí):2363毫秒
x添加@Contended注解,y不增加:
@Contended
public volatile long x;
public volatile long y;
運(yùn)行結(jié)果:
| 第一次 | 第二次 | 第三次 | 第四次 | 第五次 | 平均 |
|---|---|---|---|---|---|
| 656ms | 670ms | 664ms | 659ms | 666ms | 663ms |
平均耗時(shí):663毫秒
可以看到,性能差距3倍多。