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來保證被標(biāo)識的字段或者類不與其他字段出現(xiàn)內(nèi)存爭用。
那么什么是是內(nèi)存爭用树瞭?首先我們需要了解CPU是如何從內(nèi)存中讀取數(shù)據(jù)的赊瞬。
緩存行
為了提高IO效率,CPU每次從內(nèi)存讀取數(shù)據(jù)并不是只讀取我們需要計算的數(shù)據(jù)脊奋,而是將我們需要的數(shù)據(jù)周圍的64個字節(jié)(intel處理器的緩存行是64字節(jié))的數(shù)據(jù)一次性全部讀取到緩存中践叠。這64個字節(jié)的數(shù)據(jù)就稱為一個緩存行憔购。
假設(shè)現(xiàn)在有兩個線程都需要緩存行1(見圖)中的數(shù)據(jù)做運(yùn)算,假設(shè)CPU1需要緩存行1中的第一個字節(jié)數(shù)據(jù)做運(yùn)算血公,CPU2需要緩存行1中的第二個字節(jié)做運(yùn)算。此時CPU1和CPU2都需要將緩存行1讀取到緩存中缓熟,這樣就有可能出現(xiàn)緩存不一致現(xiàn)象累魔,為了保證緩存一致性摔笤,出現(xiàn)了很多種的緩存一致性協(xié)議,其中intel使用了MESI協(xié)議來保證緩存一致性垦写。簡單的說吕世,當(dāng)CPU1對緩存行1中的數(shù)據(jù)做了修改時,會通知CPU2梯投,告訴他數(shù)據(jù)我修改了寞冯,你那邊作廢了,需要重新從內(nèi)存讀取晚伙。反之吮龄,CPU2對數(shù)據(jù)做出修改,CPU1也需要重新讀取咆疗。這樣就會導(dǎo)致大量的IO操作漓帚,導(dǎo)致性能降低。
為了避免這種現(xiàn)象午磁,我們需要想辦法將這兩個數(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
使用額外的字段來對齊緩存行昧辽,讓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有很大概率位于同一個緩存行喇嘱。就會出現(xiàn)我們剛才所說的頻繁的重新從內(nèi)存讀取數(shù)據(jù)茉贡。如果對x變量添加了@Contended注解,則可以保證x與y在不同的緩存行者铜。
注意:如果想要@Contended注解起作用腔丧,需要在啟動時添加JVM參數(shù):
-XX:-RestrictContended
測試結(jié)果
x和y都不增加@Contended注解:
public volatile long x;
public volatile long y;
運(yùn)行結(jié)果:
第一次 | 第二次 | 第三次 | 第四次 | 第五次 | 平均 |
---|---|---|---|---|---|
2328ms | 2357ms | 2424ms | 2453ms | 2255ms | 2363ms |
平均耗時:2363毫秒
x添加@Contended注解,y不增加:
@Contended
public volatile long x;
public volatile long y;
運(yùn)行結(jié)果:
第一次 | 第二次 | 第三次 | 第四次 | 第五次 | 平均 |
---|---|---|---|---|---|
656ms | 670ms | 664ms | 659ms | 666ms | 663ms |
平均耗時:663毫秒
可以看到作烟,性能差距3倍多愉粤。