本文整理汇总了Java中java.util.concurrent.ThreadLocalRandom.nextBytes方法的典型用法代码示例。如果您正苦于以下问题:Java ThreadLocalRandom.nextBytes方法的具体用法?Java ThreadLocalRandom.nextBytes怎么用?Java ThreadLocalRandom.nextBytes使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类java.util.concurrent.ThreadLocalRandom
的用法示例。
在下文中一共展示了ThreadLocalRandom.nextBytes方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: generateExternalId
import java.util.concurrent.ThreadLocalRandom; //导入方法依赖的package包/类
/**
* Helper method to generate an "external" QueryId.
*
* This is the only method that can generate external query Id.
*
* @return generated QueryId
*/
public static ExternalId generateExternalId() {
ThreadLocalRandom r = ThreadLocalRandom.current();
// create a new internalId where the first four bytes are a growing time (each new value comes earlier in sequence).
// Last 11 bytes are random.
// last byte is set to 0
final long time = (int) (System.currentTimeMillis()/1000);
final long p1 = ((Integer.MAX_VALUE - time) << 32) + r.nextInt();
// generate a long from 7 random bytes + 1 zero byte
// I could also just generate a random long then mask it's last bit
// but not sure how this will affect it's randomness
final byte[] bytes = new byte[7];
r.nextBytes(bytes);
long p2 = 0;
for (int i = 0; i < 7; i++) {
p2 += (bytes[i] & 0xFFL) << (8 * i);
}
p2 = p2 << 8;
return ExternalId.newBuilder().setPart1(p1).setPart2(p2).build();
}