源码:
http://www.docjar.com/html/api/sun/misc/Unsafe.java.html
API文档:
http://www.docjar.com/docs/api/sun/misc/Unsafe.html
我的分类:
1.声明
2.内存的操作
3.类相关操作
4.synchronization
5.park,unpark
6.CAS
其它人的idea:
https://www.cnblogs.com/mickole/articles/3757278.html
1.首先,unsafe 类,可以分配内存,可以释放内存。
2.可以定位对象某字段的内存位置,也可以修改对象的字段值,即使它是私有的;
3.挂起与恢复
4.CAS操作
声明
first we come to the declaration of the Usafe:
you can see the function in line 83,it will check the classLoader of the class who call it, if the one who call it have class loader ,it will throw "SecurityException". and that is the way it define if it is trusted code or not ,which annotated in the line 35.
the following passage have nice explain in " how to get the unsafe".
http://mishadoff.com/blog/java-magic-part-4-sun-dot-misc-dot-unsafe/
/**
33 * A collection of methods for performing low-level, unsafe operations.
34 * Although the class and all methods are public, use of this class is
35 * limited because only trusted code can obtain instances of it.
36 *
37 * @author John R. Rose
38 * @see #getUnsafe
39 */
40
41 public final class Unsafe {
42
43 private static native void registerNatives();
44 static {
45 registerNatives();
46 sun.reflect.Reflection.registerMethodsToFilter(Unsafe.class, "getUnsafe");
47 }
48
49 private Unsafe() {}
50
51 private static final Unsafe theUnsafe = new Unsafe();
52
83 public static Unsafe getUnsafe() {
84 Class cc = sun.reflect.Reflection.getCallerClass(2);
85 if (cc.getClassLoader() != null)
86 throw new SecurityException("Unsafe");
87 return theUnsafe;
88 }
memory control
61 function explain here,the idea is all write in the annotation,include:
1.allocateMemory, reallocateMemory, freeMemory
2.get and put function
3.setMemory , copyMemory
import java.lang.reflect.Field;
import java.util.Arrays;
import sun.misc.Unsafe;
public class Test {
private static int byteArrayBaseOffset;
public static void main(String[] args) throws SecurityException,
NoSuchFieldException, IllegalArgumentException,
IllegalAccessException {
/***
* here shows how can we get the unsafe
*/
Field theUnsafe = Unsafe.class.getDeclaredField("theUnsafe");
theUnsafe.setAccessible(true);
Unsafe UNSAFE = (Unsafe) theUnsafe.get(null);
System.out.println(UNSAFE);
/** in jdk8 there is 115 function, i count it myself ,i don't make sure i have't mistake here */
Field sa = Integer.class.getDeclaredField("SIZE");
System.out.println();
long offset = UNSAFE.staticFieldOffset(sa);
int value = UNSAFE.getInt(UNSAFE.staticFieldBase(sa),offset);
System.out.println("value :" +value);
/**
* allocateMemory allocate a new memory
* reallocateMemory will resize the memory size, if it fail to resize , throws OutOfMemoryError
* freeMemory
*
* */
/** in this case is show how can we use the given memory address to put value in ,and the influence in the memory
*
* 1.as you can see ,even you just have be allocate 1 memory ,you can use more memory ,but it is not safe at all ,
* cause you can not forbid other thread or Object not to use the un-authority space,it will cause unexpected result
*
* so you know all the putXXX(long address,XXX value) and getXXX(long address) is couple
* and how to use it.
*
* there are Int,Long,Byte,Boolean,Object,Float,Double,Short,Char 9 base type ,so there is 9 base function couple. 9*2=18
*
* there a
*
* */
long am = UNSAFE.allocateMemory(1);//here i allocate a new memory
UNSAFE.putInt(am,1);//i put a 0000 0001 in the first byte.
System.out.println(UNSAFE.getInt(am)); //
UNSAFE.putInt(am+1,2);//i put 0000 0010 int the second byte
System.out.println("am+1 : "+UNSAFE.getInt(am+1)); // get the am+1 ,so it start from the second byte ,so the output here is 2
System.out.println("am+2 : "+ UNSAFE.getInt(am+2));//and here should be 0
System.out.println("am :" + UNSAFE.getInt(am));// but if i start from the first byte it should be 0000 0001 0000 0010 so the result is 2*256 + 1= 512 +1 = 513,so the out put here should be 513
UNSAFE.putInt(am+2,3);
System.out.println(UNSAFE.getByte(am));
System.out.println(UNSAFE.getByte(am+1));
System.out.println(UNSAFE.getByte(am+2));
System.out.println(UNSAFE.getByte(am+3));
System.out.println(UNSAFE.getInt(am)); //65536 * 3 + 2*256 +1 = 196608 + 512 +1 = 197121
UNSAFE.putInt(am+5,1);
System.out.println(UNSAFE.getByte(am));
System.out.println(UNSAFE.getByte(am+1));
System.out.println(UNSAFE.getByte(am+2));
System.out.println(UNSAFE.getByte(am+3));
System.out.println(UNSAFE.getByte(am+4));
System.out.println(UNSAFE.getInt(am));//so in here we can know the int take 4 bytes
UNSAFE.freeMemory(am);
/** like the case above,this case is almost the same. the only different is it use relative offset to describe the address
*
* use object + offset to define a unique address
*
* the follow case shows how it use.
* the same way , there are 9 base type ,so there is 9 base function couple, 9 * 2 = 18
* */
Integer integer = new Integer(10);
System.out.println(integer);
Field valueField = Integer.class.getDeclaredField("value");
long a = UNSAFE.objectFieldOffset(valueField);
UNSAFE.putInt(integer,a,11);
System.out.println(UNSAFE.getInt(integer,a));
System.out.println(integer);
byte[] data = new byte[10];
System.out.println(Arrays.toString(data));
byteArrayBaseOffset = UNSAFE.arrayBaseOffset(byte[].class);
System.out.println(byteArrayBaseOffset);
UNSAFE.putByte(data, byteArrayBaseOffset, (byte) 1);
UNSAFE.putByte(data, byteArrayBaseOffset + 5, (byte) 5);
System.out.println(Arrays.toString(data));
/**
* ofcourse there also provide funciton volatile
*
* Fetches a reference value from a given Java variable, with volatile
* load semantics. Otherwise identical to {@link #getObject(Object, long)}
public native Object getObjectVolatile(Object o, long offset);
* Stores a reference value into a given Java variable, with
* volatile store semantics. Otherwise identical to {@link #putObject(Object, long, Object)}
public native void putObjectVolatile(Object o, long offset, Object x);
*
* the same way , there are 9 base type ,so there is 9 base function couple, 9 * 2 = 18
*/
/**
* there is another 2 interesting function which is use to set value in batch
*
public native void setMemory(Object o, long offset, long bytes, byte value);
public native void setMemory(long address, long bytes, byte value);
* use o + offset or address to define the unique address "start postion"
* and bytes and value ,means from the "start postion" to "start postion"+bytes all the byte is set the value
*
* byte a = new byte[10]; or int[] a = new int[2]; can be realized by this function
*
*
public native void copyMemory(Object srcBase, long srcOffset,
Object destBase, long destOffset,
long bytes);
public void copyMemory(long srcAddress, long destAddress, long bytes) {
copyMemory(null, srcAddress, null, destAddress, bytes);
}
*
* so there is alse function like that
* use Base + offset or address to define the unique address "start postion" ."dest position" and copy the bytes long
* to the dest position
* */
/**
*
* util here we have kill 3 + 18*3 + 4 = 61 function
* */
}
}
appedency:
reference: http://www.jdon.com/performance/java-performance-optimizations-queue.html
使用Unsafe.putOrderedObject方法,这个方法在对低延迟代码是很有用的,它能够实现非堵塞的写入,这些写入不会被Java的JIT重新排序指令([instruction reordering](http://stackoverflow.com/questions/14321212/java-instruction-reordering-cache-in-threads)),
这样它使用快速的存储-存储(store-store) barrier,
而不是较慢的存储-加载(store-load) barrier, (用在volatile的写操作上)
这种性能提升是有代价的,虽然便宜,也就是写后结果并不会被其他线程看到,甚至是自己的线程,通常是几纳秒后被其他线程看到,这个时间比较短,所以代价可以忍受。
类似Unsafe.putOrderedObject还有unsafe.putOrderedLong等方法,unsafe.putOrderedLong比使用 volatile long要快3倍左右。.
class
804 /**
805 * Tell the VM to define a class, without security checks. By default, the
806 * class loader and protection domain come from the caller's class.
807 */
808 public native Class defineClass(String name, byte[] b, int off, int len,
809 ClassLoader loader,
810 ProtectionDomain protectionDomain);
811
812 public native Class defineClass(String name, byte[] b, int off, int len);
813
814 /**
815 * Define a class but do not make it known to the class loader or system dictionary.
816 *
817 * For each CP entry, the corresponding CP patch must either be null or have
818 * the a format that matches its tag:
819 *
820 * - Integer, Long, Float, Double: the corresponding wrapper object type from java.lang
821 *
- Utf8: a string (must have suitable syntax if used as signature or name)
822 *
- Class: any java.lang.Class object
823 *
- String: any object (not just a java.lang.String)
824 *
- InterfaceMethodRef: (NYI) a method handle to invoke on that call site's arguments
825 *
826 * @params hostClass context for linkage, access control, protection domain, and class loader
827 * @params data bytes of a class file
828 * @params cpPatches where non-null entries exist, they replace corresponding CP entries in data
829 */
830 public native Class defineAnonymousClass(Class hostClass, byte[] data, Object[] cpPatches);
public native Object allocateInstance(Class cls) throws InstantiationException;
synchronization
838 /** Lock the object. It must get unlocked via {@link #monitorExit}. */
839 public native void monitorEnter(Object o);
840
841 /**
842 * Unlock the object. It must have been locked via {@link
843 * #monitorEnter}.
844 */
845 public native void monitorExit(Object o);
846
847 /**
848 * Tries to lock the object. Returns true or false to indicate
849 * whether the lock succeeded. If it did, the object must be
850 * unlocked via {@link #monitorExit}.
851 */
852 public native boolean tryMonitorEnter(Object o);
853
854 /** Throw the exception without telling the verifier. */
855 public native void throwException(Throwable ee);
thread park unpark
960 /**
961 * Unblock the given thread blocked on park, or, if it is
962 * not blocked, cause the subsequent call to park not to
963 * block. Note: this operation is "unsafe" solely because the
964 * caller must somehow ensure that the thread has not been
965 * destroyed. Nothing special is usually required to ensure this
966 * when called from Java (in which there will ordinarily be a live
967 * reference to the thread) but this is not nearly-automatically
968 * so when calling from native code.
969 * @param thread the thread to unpark.
970 *
971 */
972 public native void unpark(Object thread);
973
974 /**
975 * Block current thread, returning when a balancing
976 * unpark occurs, or a balancing unpark has
977 * already occurred, or the thread is interrupted, or, if not
978 * absolute and time is not zero, the given time nanoseconds have
979 * elapsed, or if absolute, the given deadline in milliseconds
980 * since Epoch has passed, or spuriously (i.e., returning for no
981 * "reason"). Note: This operation is in the Unsafe class only
982 * because unpark is, so it would be strange to place it
983 * elsewhere.
984 */
985 public native void park(boolean isAbsolute, long time);
986
CAS
859 /* Atomically update Java variable to x if it is currently holding expected.
@return true if successful*/
863 public final native boolean compareAndSwapObject(Object o, long offset,
864 Object expected,
865 Object x);
872 public final native boolean compareAndSwapInt(Object o, long offset, int expected, int x);
881 public final native boolean compareAndSwapLong(Object o, long offset, long expected, long x);
import java.lang.reflect.Field;
import sun.misc.Unsafe;
public class Test {
private static Unsafe unsafe;
public static void main(String[] args) throws Exception {
try {
//通过反射获取rt.jar下的Unsafe类
Field field = Unsafe.class.getDeclaredField("theUnsafe");
field.setAccessible(true);
unsafe = (Unsafe) field.get(null);
Integer target = 12;
Field field1 = Integer.class.getDeclaredField("value");
//compareAndSwapInt方法的属性分别是:目标对象实例,目标对象属性偏移量,当前预期值,要设的值.
//compareAndSwapInt方法是通过反射修改对象的值,具体修改对象下面那个值,可以通过偏移量,对象字段的偏移量可以通过objectFieldOffset获取
System.out.println(unsafe.compareAndSwapInt(target, unsafe.objectFieldOffset(field1), 1, 10));
System.out.println(target);
System.out.println(unsafe.compareAndSwapInt(target, unsafe.objectFieldOffset(field1), 12, 10));
System.out.println(target);
} catch (Exception e) {
System.out.println("Get Unsafe instance occur error" + e);
}
}
}