温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

Java中unsafe操作的示例分析

发布时间:2021-08-23 13:52:26 来源:亿速云 阅读:125 作者:小新 栏目:编程语言

这篇文章将为大家详细讲解有关Java中unsafe操作的示例分析,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。

Unsafe是Java无锁操作的基石,在无锁并发类中都少不了它们的身影,比如ConcurrentHashMap, ConcurrentLinkedQueue, 都是由Unsafe类来实现的。相对于与Java中的锁,它基本无开销,会原地等待。

1 compareAndSwap

/**
* 比较obj的offset处内存位置中的值和期望的值,如果相同则更新。此更新是不可中断的。
* 
* @param obj 需要更新的对象
* @param offset obj中整型field的偏移量
* @param expect 希望field中存在的值
* @param update 如果期望值expect与field的当前值相同,设置filed的值为这个新值
* @return 如果field的值被更改返回true
*/
public native boolean compareAndSwapInt(Object obj, long offset, int expect, int update);

这个就是著名的CAS操作了,分为三步来做

  1. 获取obj对象中为offset的偏移值,这里假设为realVal

  2. 比较realVal和expect

  3. 如果相同,将该值更新为update,否则不更新

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

用AtomicInteger中一个经典的例子来说明:

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);
  /**确认原始值没有被其它线程修改时,再执行更新var5+var4操作*/
  } while(!this.compareAndSwapInt(var1, var2, var5, var5 + var4));
  return var5;
}

2 putOrder

/***
  * 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对象的偏移量为offset的位置修改为value,因为Java中没有内存操作,而Unsafe的这个操作正好补充了内存操作的不足。也可以用于数组操作,比如ConcurrentHashMap中就大量用到了该操作

 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];
  // 往数组下标为0的位置,写入s0: ss[0]=s0
  UNSAFE.putOrderedObject(ss, SBASE, s0); // ordered write of segments[0]

需要注意的是obj需要设置为Volatile,否则对于其它线程会不可见

3 putXxxVolatile

/***
  * 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一样,因为必须设置为Volatile,否则有什么用呢?

关于“Java中unsafe操作的示例分析”这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,使各位可以学到更多知识,如果觉得文章不错,请把它分享出去让更多的人看到。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI