1. Java.security.SecureRandomSOURCE Analysis
JDK Produce UUID The code:
public static UUID Randomuuid () {
SecureRandom ng =holder.numbergenerator;
byte[] randombytes = newbyte[16];
ng.nextbytes (randombytes);
RANDOMBYTES[6] &= 0x0f; /* Clear Version */
RANDOMBYTES[6] |=0x40; /* Set to version 4 */
RANDOMBYTES[8] &= 0x3f; /* Clear Variant */
RANDOMBYTES[8] |=0x80; /* Set to IETF variant */
Return Newuuid (randombytes);
}
the securerandom.next* method is used.
when using SecureRandom called when the next random number is generated Nextlong or nextbytes , it will eventually call SecureRandom of the nextbytes . and nextbytes is a synchronous method that creates a performance bottleneck.
Public long Nextlong () {
It ' s okay that the bottom wordremains signed.
Return ((Long) (next) << + next (32);
}
Final protected int next (int numbits) {
int numbytes = (numbits+7)/8;
byte b[] = new Byte[numbytes];
int next = 0;
nextbytes (b);
for (int i = 0; i < numbytes; i++)
Next = (Next << 8) + (B[i] & 0xFF);
Return next >>> (numbytes*8-numbits);
}
and nextbytes is a synchronous approach
Synchronized public void nextbytes (byte[] bytes) {
Securerandomspi.enginenextbytes (bytes);
}
Securerandomspi is initialized to Sun.security.provider.SecureRandom
Securerandomspi is a securerandom.nativeprng an instance of the. as you can see from the Securityprovider list,securerandom.nativeprng by Sun.security.provider.NativePRNG provides services.
Provider:set SUN Provider Property[securerandom.nativeprng/sun.security.provider.nativeprng]
Analysis OpenJDK The source code can be seen, nativeprng.enginenextbytes called the NativePRNG.RandomIO.ensureBufferValid , while Ensurebuffervalid directly from Urandom Read data:
private void Ensurebuffervalid () throws IOException {
...
Readfully (Urandomin, Urandombuffer);
...
}
through the test can be found that hotspot need to use configuration items "-djava.security.egd=file:/dev/./urandom" to get from Urandom read the data, here OpenJDK did the optimization directly from the Urandom read the data.
2. JUGSOURCE Analysis
JUG of the com.fasterxml.uuid.impl.TimeBasedGenerator.generate () can produce time-based UUID . Because in the synchronization block, only integer operations are done, so the synchronization effect is small.
The principle is that, at initialization time, some fixed data is calculated according to the network card, random number, and so on:
Public Timebasedgenerator (ethernetaddress ethaddr, Uuidtimertimer)
{
byte [] uuidbytes = new byte [16];
if (ethaddr = = null) {
ETHADDR = Ethernetaddress.constructmulticastaddress ();
}
Initialize baseline with MAC address info
_ethernetaddress = ethaddr;
_ethernetaddress. Tobytearray (Uuidbytes, 10);
and add clock sequence
int clockseq = Timer.getclocksequence ();
Uuidbytes [Uuidutil. Byte_offset_clock_sequence] = (byte) (Clockseq >> 8);
Uuidbytes [Uuidutil. Byte_offset_clock_sequence+1] = (byte) clockseq;
Long L2 = Uuidutil.gatherlong (uuidbytes, 8);
_UUIDL2 = Uuidutil.inituuidsecondlong (L2);
_timer = timer;
}
in the call Generate , depending on the current time, add a counter , generate the other half random number:
Public UUID Generate ()
{
Final Long Rawtimestamp = _timer.gettimestamp ();
Time field components is kind of shuffled, need toslice:
int clockhi = (int) (Rawtimestamp >>> 32);
int clocklo = (int) Rawtimestamp;
and Dice
int Midhi = (clockhi << 16) | (Clockhi >>>16);
Need to squeeze in type (4 MSBs in byte 6, clock hi)
Midhi &= ~0xf000; Remove high nibble of 6th byte
Midhi |= 0x1000; Type 1
Long Midhil = (long) Midhi;
Midhil = ((Midhil << +) >>> 32); To Getrid of sign extension
and reconstruct
Long L1 = (((long) Clocklo) << 32) | Midhil;
Last Detail:must Force 2 MSB-to-be ' 10 '
return new UUID (L1, _UUIDL2);
}
Public final synchronized Long Gettimestamp ()
{
Long systime = System.currenttimemillis ();
......
_clockcounter &= 0xFF;
Systime *= Kclockmultiplierl;
Systime + = Kclockoffset;// because UUID need to GregorianCalendar , this will 1970 the time of year is added to the system time
Plus Add the clock counter:
Systime + = _clockcounter;
and then increase
+ + _clockcounter;
return systime;
}
This article from "Qin like Spring Bud" blog, please be sure to keep this source http://leo01.blog.51cto.com/694903/1795447
Java UUID and SecureRandom performance detailed