当前位置: 移动技术网 > IT编程>开发语言>Java > Java中unsafe操作实例总结

Java中unsafe操作实例总结

2019年07月19日  | 移动技术网IT编程  | 我要评论

unsafe是java无锁操作的基石,在无锁并发类中都少不了它们的身影,比如concurrenthashmap, concurrentlinkedqueue, 都是由unsafe类来实现的。相对于与java中的锁,它基本无开销,会原地等待。本文主要介绍下unsafe中的主要操作。

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,否则有什么用呢?

以上就是本次给大家分享的知识点的全部内容,感谢大家对移动技术网的支持。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网