The Sun.misc.Unsafe package in Java

Source: Internet
Author: User

Chronicle Project: Https://github.com/peter-lawrey/Java-Chronicle

This project is using the MMAP mechanism to achieve efficient read and write data, claiming to write 5 to 20 million data per second.

The author has a test that writes 1 million logs for 0.234 seconds and uses Java's own logger for 7.347 seconds.


Looking at the source code of Chronicle, we found an example of cow B using unsafe to read and write memory directly, thus improving the efficiency.

See this class in detail: https://github.com/peter-lawrey/Java-Chronicle/blob/master/src/main/java/vanilla/java/chronicle/impl/ Unsafeexcerpt.java


Unsafe Package Reference: http://www.docjar.com/docs/api/sun/misc/Unsafe.html

The following example demonstrates the simple modification of a byte[] data.

This example cannot be compiled directly in Eclipse, to the properties of the project, forbidden Reference (access rules) in Java compiler,errors/warnings is set to warning.

In addition, because the Sun.misc.Unsafe package cannot be used directly, all the code uses a reflection technique to get an unsafe instance.

[Java]View Plaincopy
  1. import   java.lang.reflect.field;  
  2. import   java.util.arrays;  
  3. import   sun.misc.unsafe;  
  4.   
  5. public class  test  {  
  6.      private static int   bytearraybaseoffset;  
  7. Public Static voidMain (string[] args)throwsSecurityException,
  8. Nosuchfieldexception, IllegalArgumentException,
  9. illegalaccessexception {
  10.          field theunsafe = unsafe.class "Theunsafe"
  11.          theunsafe.setaccessible (true
  12.          Unsafe UNSAFE =  (Unsafe)  theunsafe.get (null
  13. System.out.println (UNSAFE);
  14. byte[] data =New byte[Ten];
  15. System.out.println (arrays.tostring (data));
  16.          bytearraybaseoffset = unsafe.arraybaseoffset (byte class
  17. System.out.println (Bytearraybaseoffset);
  18.          unsafe.putbyte (data, bytearraybaseoffset,  (byte Span class= "number" style= "margin:0px; padding:0px; Border:none; Color: #c00000; Background-color:inherit; " >1
  19.          unsafe.putbyte (Data, bytearraybaseoffset + 5 byte Span class= "number" style= "margin:0px; padding:0px; Border:none; Color: #c00000; Background-color:inherit; " >5
  20. System.out.println (arrays.tostring (data));
  21. }
  22. }

The Sun.misc.Unsafe package in Java

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.