最终基于UUID.java实现编写了自己的东西。请注意,我 并不是在生成UUID ,而是以我能想到的最有效的方式 生成一个
随机的32字节十六进制字符串。
实作
import java.security.SecureRandom;import java.util.UUID;public class RandomUtil { // Maxim: Copied from UUID implementation :) private static volatile SecureRandom numberGenerator = null; private static final long MSB = 0x8000000000000000L; public static String unique() { SecureRandom ng = numberGenerator; if (ng == null) { numberGenerator = ng = new SecureRandom(); } return Long.toHexString(MSB | ng.nextLong()) + Long.toHexString(MSB | ng.nextLong()); } }用法
RandomUtil.unique()
测验
我已经测试过一些输入,以确保其正常工作:
public static void main(String[] args) { System.out.println(UUID.randomUUID().toString()); System.out.println(RandomUtil.unique()); System.out.println(); System.out.println(Long.toHexString(0x8000000000000000L |21)); System.out.println(Long.toBinaryString(0x8000000000000000L |21)); System.out.println(Long.toHexString(Long.MAX_VALUE + 1));}


