Java8中的Contended注解的作用

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ù)的赊瞬。

緩存行

CPU讀取內(nèi)存數(shù)據(jù).jpg

為了提高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倍多愉粤。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市俗壹,隨后出現(xiàn)的幾起案子科汗,更是在濱河造成了極大的恐慌藻烤,老刑警劉巖绷雏,帶你破解...
    沈念sama閱讀 222,464評論 6 517
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件头滔,死亡現(xiàn)場離奇詭異,居然都是意外死亡涎显,警方通過查閱死者的電腦和手機(jī)坤检,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 95,033評論 3 399
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來期吓,“玉大人早歇,你說我怎么就攤上這事√智冢” “怎么了箭跳?”我有些...
    開封第一講書人閱讀 169,078評論 0 362
  • 文/不壞的土叔 我叫張陵,是天一觀的道長潭千。 經(jīng)常有香客問我谱姓,道長,這世上最難降的妖魔是什么刨晴? 我笑而不...
    開封第一講書人閱讀 59,979評論 1 299
  • 正文 為了忘掉前任屉来,我火速辦了婚禮,結(jié)果婚禮上狈癞,老公的妹妹穿的比我還像新娘茄靠。我一直安慰自己,他們只是感情好蝶桶,可當(dāng)我...
    茶點(diǎn)故事閱讀 69,001評論 6 398
  • 文/花漫 我一把揭開白布慨绳。 她就那樣靜靜地躺著,像睡著了一般真竖。 火紅的嫁衣襯著肌膚如雪儡蔓。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 52,584評論 1 312
  • 那天疼邀,我揣著相機(jī)與錄音喂江,去河邊找鬼。 笑死旁振,一個胖子當(dāng)著我的面吹牛获询,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播拐袜,決...
    沈念sama閱讀 41,085評論 3 422
  • 文/蒼蘭香墨 我猛地睜開眼吉嚣,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了蹬铺?” 一聲冷哼從身側(cè)響起尝哆,我...
    開封第一講書人閱讀 40,023評論 0 277
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎甜攀,沒想到半個月后秋泄,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體琐馆,經(jīng)...
    沈念sama閱讀 46,555評論 1 319
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 38,626評論 3 342
  • 正文 我和宋清朗相戀三年恒序,在試婚紗的時候發(fā)現(xiàn)自己被綠了瘦麸。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 40,769評論 1 353
  • 序言:一個原本活蹦亂跳的男人離奇死亡歧胁,死狀恐怖滋饲,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情喊巍,我是刑警寧澤屠缭,帶...
    沈念sama閱讀 36,439評論 5 351
  • 正文 年R本政府宣布,位于F島的核電站崭参,受9級特大地震影響勿她,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜阵翎,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 42,115評論 3 335
  • 文/蒙蒙 一逢并、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧郭卫,春花似錦砍聊、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,601評論 0 25
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至词疼,卻和暖如春俯树,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背贰盗。 一陣腳步聲響...
    開封第一講書人閱讀 33,702評論 1 274
  • 我被黑心中介騙來泰國打工许饿, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人舵盈。 一個月前我還...
    沈念sama閱讀 49,191評論 3 378
  • 正文 我出身青樓陋率,卻偏偏與公主長得像,于是被迫代替她去往敵國和親秽晚。 傳聞我的和親對象是個殘疾皇子瓦糟,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,781評論 2 361