Java Unsafe Common API example.

Source: Internet
Author: User
Tags cas

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.