一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務(wù)器之家:專注于服務(wù)器技術(shù)及軟件下載分享
分類導(dǎo)航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術(shù)|正則表達(dá)式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務(wù)器之家 - 編程語言 - Java教程 - Java中unsafe操作實(shí)例總結(jié)

Java中unsafe操作實(shí)例總結(jié)

2021-04-13 14:30Java教程網(wǎng) Java教程

本篇文章給大家分享了關(guān)于Java中unsafe操作的相關(guān)知識(shí)點(diǎn)以及相關(guān)的實(shí)例代碼,有需要的朋友可以學(xué)習(xí)參考下。

Unsafe是Java無鎖操作的基石,在無鎖并發(fā)類中都少不了它們的身影,比如ConcurrentHashMap, ConcurrentLinkedQueue, 都是由Unsafe類來實(shí)現(xiàn)的。相對(duì)于與Java中的鎖,它基本無開銷,會(huì)原地等待。本文主要介紹下Unsafe中的主要操作。

1 compareAndSwap

?
1
2
3
4
5
6
7
8
9
10
/**
* 比較obj的offset處內(nèi)存位置中的值和期望的值,如果相同則更新。此更新是不可中斷的。
*
* @param obj 需要更新的對(duì)象
* @param offset obj中整型field的偏移量
* @param expect 希望field中存在的值
* @param update 如果期望值expect與field的當(dāng)前值相同,設(shè)置filed的值為這個(gè)新值
* @return 如果field的值被更改返回true
*/
public native boolean compareAndSwapInt(Object obj, long offset, int expect, int update);

這個(gè)就是著名的CAS操作了,分為三步來做

  1. 獲取obj對(duì)象中為offset的偏移值,這里假設(shè)為realVal
  2. 比較realVal和expect
  3. 如果相同,將該值更新為update,否則不更新

CAS家族還包括有,compareAndSwapObject(), compareAndSwapLong(), compareAndSwapInt()等等

用AtomicInteger中一個(gè)經(jīng)典的例子來說明:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
public final int getAndAdd(int delta) { 
  return unsafe.getAndAddInt(this, valueOffset, delta);
}
 
//unsafe.getAndAddInt
public final int getAndAddInt(Object var1, long var2, int var4) {
  int var5;
  do {
  /**獲取原始值*/
    var5 = this.getIntVolatile(var1, var2);
  /**確認(rèn)原始值沒有被其它線程修改時(shí),再執(zhí)行更新var5+var4操作*/
  } while(!this.compareAndSwapInt(var1, var2, var5, var5 + var4));
  return var5;
}

2 putOrder

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
/***
  * Sets the value of the integer field at the specified offset in the
  * supplied object to the given value. This is an ordered or lazy
  * version of <code>putIntVolatile(Object,long,int)</code>, which
  * doesn't guarantee the immediate visibility of the change to other
  * threads. It is only really useful where the integer field is
  * <code>volatile</code>, and is thus expected to change unexpectedly.
  *
  * @param obj the object containing the field to modify.
  * @param offset the offset of the integer field within <code>obj</code>.
  * @param value the new value of the field.
  * @see #putIntVolatile(Object,long,int)
  */
 public native void putOrderedInt(Object obj, long offset, int value);

將obj對(duì)象的偏移量為offset的位置修改為value,因?yàn)镴ava中沒有內(nèi)存操作,而Unsafe的這個(gè)操作正好補(bǔ)充了內(nèi)存操作的不足。也可以用于數(shù)組操作,比如ConcurrentHashMap中就大量用到了該操作

?
1
2
3
4
5
6
Segment<K,V> s0 =
   new Segment<K,V>(loadFactor, (int)(cap * loadFactor),
            (HashEntry<K,V>[])new HashEntry[cap]);
 Segment<K,V>[] ss = (Segment<K,V>[])new Segment[ssize];
 // 往數(shù)組下標(biāo)為0的位置,寫入s0: ss[0]=s0
 UNSAFE.putOrderedObject(ss, SBASE, s0); // ordered write of segments[0]

需要注意的是obj需要設(shè)置為Volatile,否則對(duì)于其它線程會(huì)不可見

3 putXxxVolatile

?
1
2
3
4
5
6
7
8
9
/***
  * Sets the value of the integer field at the specified offset in the
  * supplied object to the given value, with volatile store semantics.
  *
  * @param obj the object containing the field to modify.
  * @param offset the offset of the integer field within <code>obj</code>.
  * @param value the new value of the field.
  */
 public native void putIntVolatile(Object obj, long offset, int value);

感覺和putOrderInt一樣,因?yàn)楸仨氃O(shè)置為Volatile,否則有什么用呢?

以上就是本次給大家分享的知識(shí)點(diǎn)的全部?jī)?nèi)容,感謝大家對(duì)服務(wù)器之家的支持。

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 四虎影音在线 | www视频免费 | 四虎永久在线精品免费影视 | 亚洲国产在线观看免费视频 | 2019自拍偷拍视频 | 亚洲精品国产一区二区在线 | jzjzjz日本在线观看 | 四虎网站网址 | 日本国产成人精品视频 | 免费观看一级特黄三大片视频 | 亚洲国产精品网 | 全黄一级裸片视频免费 | 亚洲天堂在线视频观看 | 四虎音影 | 2019aw网站| 精品亚洲午夜久久久久 | 午夜小视频网站 | 久久精品亚洲精品国产欧美 | 精品国产乱码久久久久久免费 | 久久偷拍免费2017 | 精品一区二区三区视频 | 天天白天天谢天天啦 | 艹的好爽 | 亚洲www视频 | 精品久久久噜噜噜久久久app | 国产91成人精品亚洲精品 | 国产欧美一区视频在线观看 | 香蕉草莓视频 | 91精品91久久久久久 | 公翁的舌尖研磨她的花蒂小说 | 国产在线98福利播放视频免费 | 第一次出血videos | 我与白丝同桌的故事h文 | 思敏1一5集国语版免费观看 | 情侣宾馆愉拍自拍视频 | 精品国产国偷自产在线观看 | 亚州男人的天堂 | 69日本人xxxx16—18 | 97在线资源站 | 日本高清免费中文字幕不卡 | 男人搡女人视频免费看 |