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
- import java.lang.reflect.field;
- import java.util.arrays;
- import sun.misc.unsafe;
-
- public class test {
- private static int bytearraybaseoffset;
- Public Static voidMain (string[] args)throwsSecurityException,
- Nosuchfieldexception, IllegalArgumentException,
- illegalaccessexception {
- field theunsafe = unsafe.class "Theunsafe"
- theunsafe.setaccessible (true
- Unsafe UNSAFE = (Unsafe) theunsafe.get (null
- System.out.println (UNSAFE);
- byte[] data =New byte[Ten];
- System.out.println (arrays.tostring (data));
- bytearraybaseoffset = unsafe.arraybaseoffset (byte class
- System.out.println (Bytearraybaseoffset);
- unsafe.putbyte (data, bytearraybaseoffset, (byte Span class= "number" style= "margin:0px; padding:0px; Border:none; Color: #c00000; Background-color:inherit; " >1
- unsafe.putbyte (Data, bytearraybaseoffset + 5 byte Span class= "number" style= "margin:0px; padding:0px; Border:none; Color: #c00000; Background-color:inherit; " >5
- System.out.println (arrays.tostring (data));
- }
- }
The Sun.misc.Unsafe package in Java