sun.misc.Unsafe类是一个功能强大,地位很重要的角色.
在并发编程领域,很多高性能的类都会封装这个Unsafe类作为成员.
我们不可以对Unsafe进行实例化,因为它的构造方法是私有的.
使用了单例模式.
private Unsafe() {}
private static final Unsafe theUnsafe = new Unsafe();
同时提供了很多方法供使用,比如我们经常说的CAS操作就是使用的compareAndSwapInt方法等.
在并发容器ConcurrentHashMap中就使用了Unsafe类.
通过U.objectFieldOffset获取指定字段的偏移量.再通过CAS操作这个偏移量上的值.(很多套路都是先获取字段的偏移量,在通过Unsafe提供的方法操作这个偏移量上的值)
Netty在实现多个生产者单个消费者的功能时,并没有使用JDK提供的阻塞队列. 而是使用了JCTools这个第三方工具类,这个工具类提供了更高性能. 这个任务就落在了org.jctools.util.UnsafeAccess这个类上.在这个类的内部封装了sun.misc.Unsafe
public class UnsafeAccess
{
public static final boolean SUPPORTS_GET_AND_SET;
public static final Unsafe UNSAFE;
static
{
Unsafe instance;
try
{
// 通过反射实例化Unsafe
final Field field = Unsafe.class.getDeclaredField("theUnsafe");
field.setAccessible(true);
instance = (Unsafe) field.get(null);
}
catch (Exception ignored)
{
// ...
}
UNSAFE = instance;
// ...
}
public static long fieldOffset(Class clz, String fieldName) throws RuntimeException
{
try
{
// 获取指定字段的偏移量
return UNSAFE.objectFieldOffset(clz.getDeclaredField(fieldName));
}
catch (NoSuchFieldException e)
{
throw new RuntimeException(e);
}
}
}
我们写个例子
import sun.misc.Unsafe;
import java.lang.reflect.Field;
public class Example {
public static final Unsafe UNSAFE;
static {
Unsafe instance = null;
try {
final Field field = Unsafe.class.getDeclaredField("theUnsafe");
field.setAccessible(true);
instance = (Unsafe) field.get(null);
} catch (Exception ignored) {
}
UNSAFE = instance;
}
private int address;
public static void main(String[] args) throws Exception {
// #1
long valueOffset = UNSAFE.objectFieldOffset(Example.class.getDeclaredField("address"));
Example example = new Example();
// #2
UNSAFE.putOrderedInt(example, valueOffset, 30);
System.out.println(example.address);
}
代码中静态代码块通过反射实例化Unsafe(因为JDK不让我们通过new实例化它).
#1是获取指定字段address的偏移量
#2给指定偏移量设置值30
我们实现了不通过setting,而是通过Unsafe这个类给字段赋值的功能.