[java] java 中Unsafe类学习
java不能直接访问操作系统底层,而是通过本地方法来访问。Unsafe类提供了硬件级别的原子操作,主要提供了以下功能:
1、通过Unsafe类可以分配内存,可以释放内存;
2、可以定位对象某字段的内存位置,也可以修改对象的字段值,即使它是私有的;
- public native long allocateMemory(long l);
- public native long reallocateMemory(long l, long l1);
- public native void freeMemory(long l);
- public final class Unsafe {
- public static final int ARRAY_INT_BASE_OFFSET;
- public static final int ARRAY_INT_INDEX_SCALE;
- public native long staticFieldOffset(Field field);
- public native int getIntVolatile(Object obj, long l);
- public native long getLong(Object obj, long l);
- public native int arrayBaseOffset(Class class1);
- public native int arrayIndexScale(Class class1);
- static
- {
- ARRAY_INT_BASE_OFFSET = theUnsafe.arrayBaseOffset([I);
- ARRAY_INT_INDEX_SCALE = theUnsafe.arrayIndexScale([I);
- }
- }
3、挂起与恢复
- public class LockSupport {
- public static void unpark(Thread thread) {
- if (thread != null)
- unsafe.unpark(thread);
- }
- public static void park(Object blocker) {
- Thread t = Thread.currentThread();
- setBlocker(t, blocker);
- unsafe.park(false, 0L);
- setBlocker(t, null);
- }
- public static void parkNanos(Object blocker, long nanos) {
- if (nanos > 0) {
- Thread t = Thread.currentThread();
- setBlocker(t, blocker);
- unsafe.park(false, nanos);
- setBlocker(t, null);
- }
- }
- public static void parkUntil(Object blocker, long deadline) {
- Thread t = Thread.currentThread();
- setBlocker(t, blocker);
- unsafe.park(true, deadline);
- setBlocker(t, null);
- }
- public static void park() {
- unsafe.park(false, 0L);
- }
- public static void parkNanos(long nanos) {
- if (nanos > 0)
- unsafe.park(false, nanos);
- }
- public static void parkUntil(long deadline) {
- unsafe.park(true, deadline);
- }
- }
4、CAS操作
- /**
- * 比较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);
//
// 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();
}
}
[java] java 中Unsafe类学习的更多相关文章
- Java安全之Unsafe类
Java安全之Unsafe类 0x00 前言 前面使用到的一些JNI编程和Javaagent等技术,其实在安全里面的运用非常的有趣和微妙,这个已经说过很多次.后面还会发现一些比较有意思的技术,比如AS ...
- JDK中Unsafe类详解
Java中Unsafe类详解 在openjdk8下看Unsafe源码 浅析Java中的原子操作 Java并发编程之LockSupport http://hg.openjdk.java.net/jdk7 ...
- Scala中的类学习
Scala中的类学习 从java了解类的情况下,了解Scala的类并不难.Scala类中的字段自动带getter和setter方法,用@BeanProperty注解生成javaBean对象的getXX ...
- Java sun.misc.Unsafe类的学习笔记
Java未开源的Unsafe类 Unsafe类可以为我们提供高效并且线程安全方式操作变量,直接和内存数据打交道. 获取Unsafe实体的方法 private static Unsafe getUnsa ...
- java swing中Timer类的学习
最近在完成学校课程的java平时作业,要实现一个计时器,包含开始.暂停以及重置三个功能.由于老师规定要用这个timer类,也就去学习了一下,顺便记录一下. 首先呢去查了一下java手册上的东西,发现t ...
- Java中Unsafe类详解
http://www.cnblogs.com/mickole/articles/3757278.html Java不能直接访问操作系统底层,而是通过本地方法来访问.Unsafe类提供了硬件级别的原子操 ...
- 【Java深入研究】8、Java中Unsafe类详解
java不能直接访问操作系统底层,而是通过本地方法来访问.Unsafe类提供了硬件级别的原子操作,主要提供了以下功能: 1.通过Unsafe类可以分配内存,可以释放内存: 类中提供的3个本地方法all ...
- java中String类学习
java中String类的相关操作如下: (1)初始化:例如,String s = “abc”; (2)length:返回字符串的长度. (3)charAT:字符操作,按照索引值获得字符串中的指定字符 ...
- Java双刃剑之Unsafe类详解
前一段时间在研究juc源码的时候,发现在很多工具类中都调用了一个Unsafe类中的方法,出于好奇就想要研究一下这个类到底有什么作用,于是先查阅了一些资料,一查不要紧,很多资料中对Unsafe的态度都是 ...
随机推荐
- 利用面向对象思想封装Konva动态进度条
1.html代码: <!DOCTYPE html> <html lang="en"> <head> <meta charset=" ...
- SpringCloud | FeignClient和Ribbon重试机制区别与联系
在spring cloud体系项目中,引入的重试机制保证了高可用的同时,也会带来一些其它的问题,如幂等操作或一些没必要的重试. 今天就来分别分析一下 FeignClient 和 Ribbon 重试机制 ...
- 【Java】初始化过程
以下程序执行的结果是: class X{ Y y=new Y(); public X(){ System.out.print("X"); } } class Y{ public Y ...
- POJ1579:Function Run Fun
Description We all love recursion! Don't we? Consider a three-parameter recursive function w(a, b, c ...
- Celery+python+redis异步执行定时任务
我之前的一篇文章中写了[Celery+django+redis异步执行任务] 博文:http://blog.csdn.net/apple9005/article/details/54236212 你会 ...
- xml大项目,增删改查
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...
- 【转】【Unity】四元数(Quaternion)和旋转
http://blog.csdn.net/candycat1992/article/details/41254799
- Hadoop的集群和优化
Hadoop的集群和优化 Hadoop集群规模小到几个节点就可以组件一个集群,大到由几千个节点组成的超大集群. 在安装Hadoop集群时,把要安装的软件解压到集群内的所有机器上,软件的安装目录也要一致 ...
- 二叉树、红黑树、伸展树、B树、B+树
好多树啊,程序猿砍树记,吼吼. 许多程序要解决的关键问题是:快速定位特定排序项的能力. 第一类:散列 第二类:字符串查找 第三类:树算法 树算法可以在辅助存储器中存储大量的数据. 二叉树.红黑树和伸展 ...
- DataGridView使用技巧八:设置单元格的ToolTip
ToolTip属性用来设置当鼠标移动到单元格上面时的提示内容. DataGridView.ShowCellToolTips=True的情况下,单元格的ToolTip可以表示出来.对于单元格窄小,无法完 ...