【揭秘】JUC并发工具包底层机制探究,Unsafe原来这么强大!

开发 前端
在程序中过度、不正确使用Unsafe类会使得程序出错的概率变大,使得Java这种安全的语言变得不再“安全”,因此对Unsafe的使用一定要慎重。java.util.concurrent.atomic包下的原子操作类,基本都是使用Unsafe实现的。

Unsafe介绍

Unsafe是位于sun.misc包下的一个类,主要提供一些用于执行低级别、不安全操作的方法,如直接访问系统内存资源、自主管理内存资源等,这些方法在提升Java运行效率、增强Java语言底层资源操作能力方面起到了很大的作用。但由于Unsafe类使得Java语言拥有了类似C语言指针一样操作内存空间的能力,这无疑也增加了程序发生相关指针问题的风险。在程序中过度、不正确使用Unsafe类会使得程序出错的概率变大,使得Java这种安全的语言变得不再“安全”,因此对Unsafe的使用一定要慎重。java.util.concurrent.atomic包下的原子操作类,基本都是使用Unsafe实现的。

Unsafe提供的API大致可分为内存操作、CAS、Class、对象操作、线程、系统信息获取、内存屏障、数组操作等几类。

内存相关

图片图片

CAS相关

图片图片

java.util.concurrent.atomic包中的原子类基本都用的Unsafe

private static final Unsafe unsafe = Unsafe.getUnsafe();
private static final long valueOffset;
static {
  try {
    valueOffset = unsafe.objectFieldOffset(AtomicInteger.class.getDeclaredField("value"));
  } catch (Exception ex) { throw new Error(ex); }
}
public final int getAndSet(int newValue) {
  return unsafe.getAndSetInt(this, valueOffset, newValue);
}

线程相关

图片图片

LockSupport类中有应用unpark,park

public static void park(Object blocker) {
  Thread t = Thread.currentThread();
  setBlocker(t, blocker);
  UNSAFE.park(false, 0L);
  setBlocker(t, null);
}
public static void unpark(Thread thread) {
  if (thread != null)
    UNSAFE.unpark(thread);
}

Class相关

图片图片

对象操作相关

图片图片

图片图片

系统相关

图片图片

内存屏障

图片图片

loadFence:保证在这个屏障之前的所有读操作都已经完成。
storeFence:保证在这个屏障之前的所有写操作都已经完成。fullFence:保证在这个屏障之前的所有读写操作都已经完成。

在java8中 有这个StampedLock类,该类中应用了内存屏障功能。

private static final sun.misc.Unsafe U;
static {
  try {
    U = sun.misc.Unsafe.getUnsafe();
  } catch (Exception e) {
    throw new Error(e);
  }
}
public boolean validate(long stamp) {
  U.loadFence();
  return (stamp & SBITS) == (state & SBITS);
}

Unsafe.java

public final class Unsafe {


  private static native void registerNatives();
  static {
    registerNatives();
    sun.reflect.Reflection.registerMethodsToFilter(Unsafe.class, "getUnsafe");
  }


  private Unsafe() {}


  private static final Unsafe theUnsafe = new Unsafe();
  // ...
}

获取Unsafe实例

Unsafe类是final且是单例的,并且theUnsafe字段是private;通过如下方法获取实例。

方法1

Field theUnsafe = Unsafe.class.getDeclaredField("theUnsafe") ;
theUnsafe.setAccessible(true) ;
Unsafe unsafe = (Unsafe) theUnsafe.get(null) ;

方法2

private static Unsafe unsafe = null ;


static {
  try {
    Constructor<Unsafe> cons = Unsafe.class.getDeclaredConstructor() ;
    cons.setAccessible(true) ;
    unsafe = cons.newInstance() ;
  } catch (Exception e) {
    e.printStackTrace();
  }
}

Unsafe简单应用

int i = 0 ;


public static void main(String[] args) throws Exception {
  UnsafeDemo d = new UnsafeDemo() ;
  // 获取Unsafe实例
  Field theUnsafe = Unsafe.class.getDeclaredField("theUnsafe") ;
  theUnsafe.setAccessible(true) ;
  Unsafe unsafe = (Unsafe) theUnsafe.get(null) ;
  // 获取类的实例变量
  Field f = UnsafeDemo.class.getDeclaredField("i") ;
  // 获取字段相对Java对象的"起始地址"的偏移量
  long fieldOffset = unsafe.objectFieldOffset(f) ;
  System.out.println(fieldOffset) ;
  // 设置值
  boolean success = unsafe.compareAndSwapInt(d, fieldOffset, 0, 10) ;
  System.out.println(success) ;
  System.out.println(d.i) ;
}

Unsafe对象操作

private static Unsafe unsafe = null ;


static {
try {
    Constructor<Unsafe> cons = Unsafe.class.getDeclaredConstructor() ;
    cons.setAccessible(true) ;
    unsafe = cons.newInstance() ;
  } catch (Exception e) {
    e.printStackTrace();
  }
}
public static void allocate() {
  try {
    Person p = (Person)unsafe.allocateInstance(Person.class) ;
    p.setId("s001");
    System.out.println(p.getValue()) ;
    System.out.println(p.getId()) ;
  } catch (Exception e) {
    e.printStackTrace();
  }
}

执行结果:

图片图片

对象操作2:

private Person p = new Person("1", "张三") ;


public static void main(String[] args) throws Exception {
  UnSafeObjectDemo d = new UnSafeObjectDemo() ;
  Field field = Unsafe.class.getDeclaredField("theUnsafe") ;
  field.setAccessible(true) ;
  Unsafe unsafe = (Unsafe) field.get(null) ;
  Field f = d.getClass().getDeclaredField("p") ;
  long offset = unsafe.objectFieldOffset(f) ;
  System.out.println(offset) ;
  boolean res = unsafe.compareAndSwapObject(d, offset, d.p, new Person("2", "李四")) ;
  System.out.println(res) ;
  System.out.println(d.p.getName()) ;
}

图片图片

Unsafe创建对象

当不知道即将使用的对象有何构造函数,或是不想使用现有对象的构造函数创建对象时,可以通过如下方式:

Constructor<Teacher> cons = (Constructor<Teacher>) ReflectionFactory
    .getReflectionFactory()
    .newConstructorForSerialization(Teacher.class, Object.class.getConstructor());
cons.setAccessible(true) ;
Teacher t = cons.newInstance() ;
System.out.println(t) ;

Unsafe简单实现原子操作类

public class AtomicCount {


  private static Unsafe unsafe ;


  private int value ;
  private static long valueOffset ;


  static {
    try {
      Field theUnsafe = Unsafe.class.getDeclaredField("theUnsafe") ;
      theUnsafe.setAccessible(true) ;
      unsafe = (Unsafe) theUnsafe.get(null) ;


      Field f = AtomicCount.class.getDeclaredField("value") ;
      valueOffset = unsafe.objectFieldOffset(f) ;
    } catch (Exception e) {
      e.printStackTrace();
    }
  }


  public AtomicCount(int value) {
    this.value = value ;
  }


  public final int get() {
    return value;
  }


  public final int getAndIncrement() {
    return unsafe.getAndAddInt(this, valueOffset, 1);
  }


}

完毕!!!

责任编辑:武晓燕 来源: Spring全家桶实战案例源码
相关推荐

2009-11-17 14:39:59

PHP工具包

2009-02-03 14:00:20

PHP运行PHP调用PHP原理

2016-08-12 09:33:38

2020-12-15 15:08:17

工具Java线程

2020-12-15 07:54:40

工具Hutoolgithub

2022-04-09 10:02:59

HTTPLInux开源

2021-04-26 10:24:52

Linux 开发操作系统

2021-04-19 07:35:01

Linuxhistory命令

2018-05-03 09:03:16

微软工具包Windows

2021-11-17 10:16:39

Kodex隐私安全工程工具包

2009-04-03 11:12:43

PalmwebOS开发

2011-04-18 09:46:58

Windows工具包活动目录MPS报告

2020-02-10 16:07:42

工具包

2009-09-16 08:34:17

Windows Emb

2022-06-01 12:04:02

项目Webpack

2023-02-15 08:26:29

iOS工具

2021-04-19 05:42:51

Mmap文件系统

2009-01-03 15:11:05

ibmdwLinux

2016-02-16 13:21:33

2010-02-23 10:04:16

Silverlight
点赞
收藏

51CTO技术栈公众号