博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
[java] java 中Unsafe类学习
阅读量:6375 次
发布时间:2019-06-23

本文共 14782 字,大约阅读时间需要 49 分钟。

java不能直接访问操作系统底层,而是通过本地方法来访问。Unsafe类提供了硬件级别的原子操作,主要提供了以下功能:

1、通过Unsafe类可以分配内存,可以释放内存;

类中提供的3个本地方法allocateMemory、reallocateMemory、freeMemory分别用于分配内存,扩充内存和释放内存,与C语言中的3个方法对应。

2、可以定位对象某字段的内存位置,也可以修改对象的字段值,即使它是私有的;

 
  1. public native long allocateMemory(long l);  
  2. public native long reallocateMemory(long l, long l1);  
  3. public native void freeMemory(long l);  
字段的定位:
JAVA中对象的字段的定位可能通过staticFieldOffset方法实现,该方法返回给定field的内存地址偏移量,这个值对于给定的filed是唯一的且是固定不变的。
getIntVolatile方法获取对象中offset偏移地址对应的整型field的值,支持volatile load语义。
getLong方法获取对象中offset偏移地址对应的long型field的值
数组元素定位:
Unsafe类中有很多以BASE_OFFSET结尾的常量,比如ARRAY_INT_BASE_OFFSET,ARRAY_BYTE_BASE_OFFSET等,这些常量值是通过arrayBaseOffset方法得到的。arrayBaseOffset方法是一个本地方法,可以获取数组第一个元素的偏移地址。Unsafe类中还有很多以INDEX_SCALE结尾的常量,比如 ARRAY_INT_INDEX_SCALE , ARRAY_BYTE_INDEX_SCALE等,这些常量值是通过arrayIndexScale方法得到的。arrayIndexScale方法也是一个本地方法,可以获取数组的转换因子,也就是数组中元素的增量地址。将arrayBaseOffset与arrayIndexScale配合使用,可以定位数组中每个元素在内存中的位置。
[java] 
 
  1. public final class Unsafe {  
  2.     public static final int ARRAY_INT_BASE_OFFSET;  
  3.     public static final int ARRAY_INT_INDEX_SCALE;  
  4.   
  5.     public native long staticFieldOffset(Field field);  
  6.     public native int getIntVolatile(Object obj, long l);  
  7.     public native long getLong(Object obj, long l);  
  8.     public native int arrayBaseOffset(Class class1);  
  9.     public native int arrayIndexScale(Class class1);  
  10.   
  11.     static   
  12.     {  
  13.         ARRAY_INT_BASE_OFFSET = theUnsafe.arrayBaseOffset([I);  
  14.         ARRAY_INT_INDEX_SCALE = theUnsafe.arrayIndexScale([I);  
  15.     }  
  16. }  

3、挂起与恢复

将一个线程进行挂起是通过park方法实现的,调用 park后,线程将一直阻塞直到超时或者中断等条件出现。unpark可以终止一个挂起的线程,使其恢复正常。整个并发框架中对线程的挂起操作被封装在 LockSupport类中,LockSupport类中有各种版本pack方法,但最终都调用了Unsafe.park()方法。
[java] 
 
  1. public class LockSupport {  
  2.     public static void unpark(Thread thread) {  
  3.         if (thread != null)  
  4.             unsafe.unpark(thread);  
  5.     }  
  6.   
  7.     public static void park(Object blocker) {  
  8.         Thread t = Thread.currentThread();  
  9.         setBlocker(t, blocker);  
  10.         unsafe.park(false, 0L);  
  11.         setBlocker(t, null);  
  12.     }  
  13.   
  14.     public static void parkNanos(Object blocker, long nanos) {  
  15.         if (nanos > 0) {  
  16.             Thread t = Thread.currentThread();  
  17.             setBlocker(t, blocker);  
  18.             unsafe.park(false, nanos);  
  19.             setBlocker(t, null);  
  20.         }  
  21.     }  
  22.   
  23.     public static void parkUntil(Object blocker, long deadline) {  
  24.         Thread t = Thread.currentThread();  
  25.         setBlocker(t, blocker);  
  26.         unsafe.park(true, deadline);  
  27.         setBlocker(t, null);  
  28.     }  
  29.   
  30.     public static void park() {  
  31.         unsafe.park(false, 0L);  
  32.     }  
  33.   
  34.     public static void parkNanos(long nanos) {  
  35.         if (nanos > 0)  
  36.             unsafe.park(false, nanos);  
  37.     }  
  38.   
  39.     public static void parkUntil(long deadline) {  
  40.         unsafe.park(true, deadline);  
  41.     }  
  42. }  
4、CAS操作
是通过compareAndSwapXXX方法实现的
[java] 
 
  1. /** 
  2. * 比较obj的offset处内存位置中的值和期望的值,如果相同则更新。此更新是不可中断的。 
  3. *  
  4. * @param obj 需要更新的对象 
  5. * @param offset obj中整型field的偏移量 
  6. * @param expect 希望field中存在的值 
  7. * @param update 如果期望值expect与field的当前值相同,设置filed的值为这个新值 
  8. * @return 如果field的值被更改返回true 
  9. */  
  10. public native boolean compareAndSwapInt(Object obj, long offset, int expect, int update);  
 
CAS操作有3个操作数,内存值M,预期值E,新值U,如果M==E,则将内存值修改为B,否则啥都不做。
 
另外附上Unsafe 源代码:
//// Source code recreated from a .class file by IntelliJ IDEA// (powered by Fernflower decompiler)//package sun.misc;import java.lang.reflect.Field;import java.lang.reflect.Modifier;import java.security.ProtectionDomain;import sun.reflect.CallerSensitive;import sun.reflect.Reflection;public final class Unsafe {    private static final Unsafe theUnsafe;    public static final int INVALID_FIELD_OFFSET = -1;    public static final int ARRAY_BOOLEAN_BASE_OFFSET;    public static final int ARRAY_BYTE_BASE_OFFSET;    public static final int ARRAY_SHORT_BASE_OFFSET;    public static final int ARRAY_CHAR_BASE_OFFSET;    public static final int ARRAY_INT_BASE_OFFSET;    public static final int ARRAY_LONG_BASE_OFFSET;    public static final int ARRAY_FLOAT_BASE_OFFSET;    public static final int ARRAY_DOUBLE_BASE_OFFSET;    public static final int ARRAY_OBJECT_BASE_OFFSET;    public static final int ARRAY_BOOLEAN_INDEX_SCALE;    public static final int ARRAY_BYTE_INDEX_SCALE;    public static final int ARRAY_SHORT_INDEX_SCALE;    public static final int ARRAY_CHAR_INDEX_SCALE;    public static final int ARRAY_INT_INDEX_SCALE;    public static final int ARRAY_LONG_INDEX_SCALE;    public static final int ARRAY_FLOAT_INDEX_SCALE;    public static final int ARRAY_DOUBLE_INDEX_SCALE;    public static final int ARRAY_OBJECT_INDEX_SCALE;    public static final int ADDRESS_SIZE;    private static native void registerNatives();    private Unsafe() {    }    @CallerSensitive    public static Unsafe getUnsafe() {        Class var0 = Reflection.getCallerClass();        if(var0.getClassLoader() != null) {            throw new SecurityException("Unsafe");        } else {            return theUnsafe;        }    }    public native int getInt(Object var1, long var2);    public native void putInt(Object var1, long var2, int var4);    public native Object getObject(Object var1, long var2);    public native void putObject(Object var1, long var2, Object var4);    public native boolean getBoolean(Object var1, long var2);    public native void putBoolean(Object var1, long var2, boolean var4);    public native byte getByte(Object var1, long var2);    public native void putByte(Object var1, long var2, byte var4);    public native short getShort(Object var1, long var2);    public native void putShort(Object var1, long var2, short var4);    public native char getChar(Object var1, long var2);    public native void putChar(Object var1, long var2, char var4);    public native long getLong(Object var1, long var2);    public native void putLong(Object var1, long var2, long var4);    public native float getFloat(Object var1, long var2);    public native void putFloat(Object var1, long var2, float var4);    public native double getDouble(Object var1, long var2);    public native void putDouble(Object var1, long var2, double var4);    /** @deprecated */    @Deprecated    public int getInt(Object var1, int var2) {        return this.getInt(var1, (long)var2);    }    /** @deprecated */    @Deprecated    public void putInt(Object var1, int var2, int var3) {        this.putInt(var1, (long)var2, var3);    }    /** @deprecated */    @Deprecated    public Object getObject(Object var1, int var2) {        return this.getObject(var1, (long)var2);    }    /** @deprecated */    @Deprecated    public void putObject(Object var1, int var2, Object var3) {        this.putObject(var1, (long)var2, var3);    }    /** @deprecated */    @Deprecated    public boolean getBoolean(Object var1, int var2) {        return this.getBoolean(var1, (long)var2);    }    /** @deprecated */    @Deprecated    public void putBoolean(Object var1, int var2, boolean var3) {        this.putBoolean(var1, (long)var2, var3);    }    /** @deprecated */    @Deprecated    public byte getByte(Object var1, int var2) {        return this.getByte(var1, (long)var2);    }    /** @deprecated */    @Deprecated    public void putByte(Object var1, int var2, byte var3) {        this.putByte(var1, (long)var2, var3);    }    /** @deprecated */    @Deprecated    public short getShort(Object var1, int var2) {        return this.getShort(var1, (long)var2);    }    /** @deprecated */    @Deprecated    public void putShort(Object var1, int var2, short var3) {        this.putShort(var1, (long)var2, var3);    }    /** @deprecated */    @Deprecated    public char getChar(Object var1, int var2) {        return this.getChar(var1, (long)var2);    }    /** @deprecated */    @Deprecated    public void putChar(Object var1, int var2, char var3) {        this.putChar(var1, (long)var2, var3);    }    /** @deprecated */    @Deprecated    public long getLong(Object var1, int var2) {        return this.getLong(var1, (long)var2);    }    /** @deprecated */    @Deprecated    public void putLong(Object var1, int var2, long var3) {        this.putLong(var1, (long)var2, var3);    }    /** @deprecated */    @Deprecated    public float getFloat(Object var1, int var2) {        return this.getFloat(var1, (long)var2);    }    /** @deprecated */    @Deprecated    public void putFloat(Object var1, int var2, float var3) {        this.putFloat(var1, (long)var2, var3);    }    /** @deprecated */    @Deprecated    public double getDouble(Object var1, int var2) {        return this.getDouble(var1, (long)var2);    }    /** @deprecated */    @Deprecated    public void putDouble(Object var1, int var2, double var3) {        this.putDouble(var1, (long)var2, var3);    }    public native byte getByte(long var1);    public native void putByte(long var1, byte var3);    public native short getShort(long var1);    public native void putShort(long var1, short var3);    public native char getChar(long var1);    public native void putChar(long var1, char var3);    public native int getInt(long var1);    public native void putInt(long var1, int var3);    public native long getLong(long var1);    public native void putLong(long var1, long var3);    public native float getFloat(long var1);    public native void putFloat(long var1, float var3);    public native double getDouble(long var1);    public native void putDouble(long var1, double var3);    public native long getAddress(long var1);    public native void putAddress(long var1, long var3);    public native long allocateMemory(long var1);    public native long reallocateMemory(long var1, long var3);    public native void setMemory(Object var1, long var2, long var4, byte var6);    public void setMemory(long var1, long var3, byte var5) {        this.setMemory((Object)null, var1, var3, var5);    }    public native void copyMemory(Object var1, long var2, Object var4, long var5, long var7);    public void copyMemory(long var1, long var3, long var5) {        this.copyMemory((Object)null, var1, (Object)null, var3, var5);    }    public native void freeMemory(long var1);    /** @deprecated */    @Deprecated    public int fieldOffset(Field var1) {        return Modifier.isStatic(var1.getModifiers())?(int)this.staticFieldOffset(var1):(int)this.objectFieldOffset(var1);    }    /** @deprecated */    @Deprecated    public Object staticFieldBase(Class var1) {        Field[] var2 = var1.getDeclaredFields();        for(int var3 = 0; var3 < var2.length; ++var3) {            if(Modifier.isStatic(var2[var3].getModifiers())) {                return this.staticFieldBase(var2[var3]);            }        }        return null;    }    public native long staticFieldOffset(Field var1);    public native long objectFieldOffset(Field var1);    public native Object staticFieldBase(Field var1);    public native boolean shouldBeInitialized(Class
var1); public native void ensureClassInitialized(Class var1); public native int arrayBaseOffset(Class var1); public native int arrayIndexScale(Class var1); public native int addressSize(); public native int pageSize(); public native Class defineClass(String var1, byte[] var2, int var3, int var4, ClassLoader var5, ProtectionDomain var6); /** @deprecated */ @Deprecated @CallerSensitive public native Class defineClass(String var1, byte[] var2, int var3, int var4); public native Class defineAnonymousClass(Class var1, byte[] var2, Object[] var3); public native Object allocateInstance(Class var1) throws InstantiationException; public native void monitorEnter(Object var1); public native void monitorExit(Object var1); public native boolean tryMonitorEnter(Object var1); public native void throwException(Throwable var1); public final native boolean compareAndSwapObject(Object var1, long var2, Object var4, Object var5); public final native boolean compareAndSwapInt(Object var1, long var2, int var4, int var5); public final native boolean compareAndSwapLong(Object var1, long var2, long var4, long var6); public native Object getObjectVolatile(Object var1, long var2); public native void putObjectVolatile(Object var1, long var2, Object var4); public native int getIntVolatile(Object var1, long var2); public native void putIntVolatile(Object var1, long var2, int var4); public native boolean getBooleanVolatile(Object var1, long var2); public native void putBooleanVolatile(Object var1, long var2, boolean var4); public native byte getByteVolatile(Object var1, long var2); public native void putByteVolatile(Object var1, long var2, byte var4); public native short getShortVolatile(Object var1, long var2); public native void putShortVolatile(Object var1, long var2, short var4); public native char getCharVolatile(Object var1, long var2); public native void putCharVolatile(Object var1, long var2, char var4); public native long getLongVolatile(Object var1, long var2); public native void putLongVolatile(Object var1, long var2, long var4); public native float getFloatVolatile(Object var1, long var2); public native void putFloatVolatile(Object var1, long var2, float var4); public native double getDoubleVolatile(Object var1, long var2); public native void putDoubleVolatile(Object var1, long var2, double var4); public native void putOrderedObject(Object var1, long var2, Object var4); public native void putOrderedInt(Object var1, long var2, int var4); public native void putOrderedLong(Object var1, long var2, long var4); public native void unpark(Object var1); public native void park(boolean var1, long var2); public native int getLoadAverage(double[] var1, int var2); static { registerNatives(); Reflection.registerMethodsToFilter(Unsafe.class, new String[]{"getUnsafe"}); theUnsafe = new Unsafe(); ARRAY_BOOLEAN_BASE_OFFSET = theUnsafe.arrayBaseOffset(boolean[].class); ARRAY_BYTE_BASE_OFFSET = theUnsafe.arrayBaseOffset(byte[].class); ARRAY_SHORT_BASE_OFFSET = theUnsafe.arrayBaseOffset(short[].class); ARRAY_CHAR_BASE_OFFSET = theUnsafe.arrayBaseOffset(char[].class); ARRAY_INT_BASE_OFFSET = theUnsafe.arrayBaseOffset(int[].class); ARRAY_LONG_BASE_OFFSET = theUnsafe.arrayBaseOffset(long[].class); ARRAY_FLOAT_BASE_OFFSET = theUnsafe.arrayBaseOffset(float[].class); ARRAY_DOUBLE_BASE_OFFSET = theUnsafe.arrayBaseOffset(double[].class); ARRAY_OBJECT_BASE_OFFSET = theUnsafe.arrayBaseOffset(Object[].class); ARRAY_BOOLEAN_INDEX_SCALE = theUnsafe.arrayIndexScale(boolean[].class); ARRAY_BYTE_INDEX_SCALE = theUnsafe.arrayIndexScale(byte[].class); ARRAY_SHORT_INDEX_SCALE = theUnsafe.arrayIndexScale(short[].class); ARRAY_CHAR_INDEX_SCALE = theUnsafe.arrayIndexScale(char[].class); ARRAY_INT_INDEX_SCALE = theUnsafe.arrayIndexScale(int[].class); ARRAY_LONG_INDEX_SCALE = theUnsafe.arrayIndexScale(long[].class); ARRAY_FLOAT_INDEX_SCALE = theUnsafe.arrayIndexScale(float[].class); ARRAY_DOUBLE_INDEX_SCALE = theUnsafe.arrayIndexScale(double[].class); ARRAY_OBJECT_INDEX_SCALE = theUnsafe.arrayIndexScale(Object[].class); ADDRESS_SIZE = theUnsafe.addressSize(); }}

 

转载地址:http://cctqa.baihongyu.com/

你可能感兴趣的文章
机器学习实验笔记
查看>>
不同工具查看代码分支diff的差异
查看>>
一文 | 跨域及其解决方案
查看>>
白话Java I/O模型
查看>>
[TsAdmin]--一款基于Vue.js+Element UI的单页无刷新(无iframe)多选项卡的后台管理系统模板...
查看>>
排列组合技术
查看>>
哈工大发明“电子体毛”,让机器人学会“敏感”
查看>>
上传一张照片,让算法告诉你是否患有抑郁症
查看>>
VR厂商唯晶科技获2800万C+轮融资,曾开发过游戏《圣女之歌》
查看>>
Countly 19.02.1 发布,实时移动和 web 分析报告平台
查看>>
TCP连接中time_wait在开发中的影响-搜人以鱼不如授之以渔
查看>>
Oracle数据库机出新帮助不同规模企业迈向云端
查看>>
前端通信:ajax设计方案(六)--- 全局配置、请求格式拓展和优化、请求二进制类型、浏览器错误搜集以及npm打包发布...
查看>>
Android捕获监听Home键、最近任务列表键
查看>>
微服务分布式企业框架 Springmvc+mybatis+shiro+Dubbo+ZooKeeper+Redis+KafKa
查看>>
word2vec原理(三) 基于Negative Sampling的模型
查看>>
被《时代周刊》选为年度最佳发明,PS VR靠的竟然是价格
查看>>
通用唯一标识码UUID的介绍及使用。
查看>>
spring笔记--依赖注入之针对不同类型变量的几种注入方式
查看>>
Java爬虫——网易云热评爬取
查看>>