日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

A Faster Volatile

發布時間:2024/9/30 编程问答 35 豆豆
生活随笔 收集整理的這篇文章主要介紹了 A Faster Volatile 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

在java中使用volatile修飾變量,在某些多線程讀寫情況下可以避免顯示加鎖,但是volatile實現的原理是在讀寫volatile變量的上下文中添加內存屏障,禁止cpu對指令進行調序優化,并且保證,如果一個線程寫了這個volatile變量,那么在寫之前,這個變量肯定能獲得其最新值,在寫之后其他線程就能立即獲得其最新值。


從中可以看到,對volatile變量進行讀寫的cost還是比較大,有沒有更快的方法?


用java自帶的原子類型代替volatile,

使用其中的lazySet更新變量,其他線程在幾個納秒之后才能知道更新,而不是立即,避免了較多的通信

也就是說,如果調用get,可能獲得是之前的舊值,一,這概率太低,二,對于讀線程而言,獲得一個舊值,在語義上也沒有錯誤。

本人推測,如果其他讀線程在調用lazySet的時候,肯定是在最新值的基礎上更改的。


http://robsjava.blogspot.com/2013/06/a-faster-volatile.html

One of the methods in?AtomicReference?is?lazySet, under the covers it calls unsafe.putOrderedObject

public final void lazySet(V newValue) {
? ? ? ? ? ?unsafe.putOrderedObject(this, valueOffset, newValue);?
}


Unsafe.putOrderedObject is useful for tuning ultra low latency?code. It allows us to create non-blocking code with guaranteed writes.?These writes will not be re-orderd by?instruction reordering. Under the covers it uses the faster?store-store barrier, rather than the the slower?store-load barrier,?which is used when doing a volatile?write.
This performance improvement comes at a cost, as the writes may not immediately be visible to other threads. However, in practice, these writes usually become visible to the other threads within a few nanoseconds. Since this approach does not stall the bus, it usually performs better.

StoreStore Barrier

A StoreStore barrier effectively prevents reordering of stores performed before the barrier with stores performed after the barrier.

StoreLoad Barrier

A StoreLoad barrier ensures that all stores performed before the barrier are visible to other processors, and that all loads performed after the barrier receive the latest value that is visible at the time of the barrier.


總結

以上是生活随笔為你收集整理的A Faster Volatile的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。