本文整理匯總了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();
}