1 Thread Suspend recovery
2 CAS operations
2 Direct Memory operations (non-JVM memory)
3 Instantiation of objects
4 Direct manipulation of object properties
5 Direct manipulation arrays
The sample code is as follows
Public classPlayer {PrivateString name; Private intAge ; PublicString GetName () {returnname; } Public voidsetName (String name) { This. Name =name; } Public intGetage () {returnAge ; } Public voidSetage (intAge ) { This. Age =Age ; } }
ImportJava.lang.reflect.Field;ImportCom.alibaba.fastjson.JSON;ImportCom.guo.util.Util;ImportSun.misc.Unsafe;/*** Unsafe Operation Array *@authorGuojunwei **/ Public classTest1 { Public Staticunsafe unsafe =Util.getunsafe (); Public Static voidMain (string[] args)throwsException {//function1 (); //function2 (); //Function3 (); //Function4 (); //function5 ();Function6 (); } /*** Park (): Method return condition * 1 now called the Unpark method (multiple calls per calculation) * 2 current thread is interrupted * 3 when Park is false: time block to Unit nanosecond * 4 When Park is true: the time is absolute Time (1970) year due unit milliseconds*/ Public Static voidFunction6 () {System.out.println ("Start"); LongTime = System.currenttimemillis () +3000l; Unsafe.park (true, time); System.out.println ("End"); } /*** CAs operation *@throwsException*/ Public Static voidFunction5 ()throwsException {player player= (player) unsafe.allocateinstance (player).class); Field Age= Player.getclass (). Getdeclaredfield ("Age"); LongAddressage =Unsafe.objectfieldoffset (age); Unsafe.compareandswapint (Player, Addressage,0, 100); System.out.println (Player.getage ()); } /*** Directly allocate memory address: memory Management*/ Public Static voidFunction4 () {//allocating 100 bytes of memory returns the initial address LongAddress = unsafe.allocatememory (100); //write value to allocated memory addressUnsafe.putint (Address, 55); //Get ValueSystem.out.println (Unsafe.getint (address)); //allocating 100 bytes of memory returns the initial address LongAddress1 = unsafe.allocatememory (100); //Copy Memory ValueUnsafe.copymemory (Address, Address1, 4); System.out.println (Unsafe.getint (Address1)); //Freeing Memoryunsafe.freememory (address); Unsafe.freememory (ADDRESS1); } /*** Action object property value *@throwsException*/ Public Static voidFunction3 ()throwsException {player player= (player) unsafe.allocateinstance (player).class); Field FieldName= Player.getclass (). Getdeclaredfield ("name"); Field Fieldage= Player.getclass (). Getdeclaredfield ("Age"); LongFilenameaddres =Unsafe.objectfieldoffset (fieldName); LongFileageaddres =Unsafe.objectfieldoffset (fieldage); Unsafe.putobjectvolatile (Player, Filenameaddres,"Wangwu"); Unsafe.putint (Player,fileageaddres,100); System.out.println (Player.getage ()+" "+player.getname ()); } /*** Instantiate Object *@throwsinstantiationexception*/ Public Static voidFunction2 ()throwsinstantiationexception {player player= (player) unsafe.allocateinstance (player).class); Player.setage (100); Player.setname ("Zhangshan"); System.out.println (Player.getage ()+" "+player.getname ()); } /*** operation of the array*/ Public Static voidfunction1 () {int[] num =New int[7]; //The starting address of the array Longadress = Unsafe.arraybaseoffset (int[].class); //Block Size Longindex = Unsafe.arrayindexscale (int[].class); Unsafe.putint (num, adress,1); Unsafe.putint (num, adress+index, 2); Unsafe.putint (num, adress+index+index, 3); Unsafe.putint (num, adress+index+index+index, 4); SYSTEM.OUT.PRINTLN (json.tojsonstring (num)); }}
Java Unsafe Common API example.