提交 9871ad68 编写于 作者: P psandoz

8066397: Remove network-related seed initialization code in ThreadLocal/SplittableRandom

Reviewed-by: alanb, dl, chegar, rriggs, shade
上级 203d62fa
...@@ -25,7 +25,6 @@ ...@@ -25,7 +25,6 @@
package java.util; package java.util;
import java.net.NetworkInterface;
import java.util.concurrent.atomic.AtomicLong; import java.util.concurrent.atomic.AtomicLong;
import java.util.function.IntConsumer; import java.util.function.IntConsumer;
import java.util.function.LongConsumer; import java.util.function.LongConsumer;
...@@ -140,11 +139,10 @@ public final class SplittableRandom { ...@@ -140,11 +139,10 @@ public final class SplittableRandom {
* other cases, this split must be performed in a thread-safe * other cases, this split must be performed in a thread-safe
* manner, so we use an AtomicLong to represent the seed rather * manner, so we use an AtomicLong to represent the seed rather
* than use an explicit SplittableRandom. To bootstrap the * than use an explicit SplittableRandom. To bootstrap the
* defaultGen, we start off using a seed based on current time and * defaultGen, we start off using a seed based on current time
* network interface address unless the java.util.secureRandomSeed * unless the java.util.secureRandomSeed property is set. This
* property is set. This serves as a slimmed-down (and insecure) * serves as a slimmed-down (and insecure) variant of SecureRandom
* variant of SecureRandom that also avoids stalls that may occur * that also avoids stalls that may occur when using /dev/random.
* when using /dev/random.
* *
* It is a relatively simple matter to apply the basic design here * It is a relatively simple matter to apply the basic design here
* to use 128 bit seeds. However, emulating 128bit arithmetic and * to use 128 bit seeds. However, emulating 128bit arithmetic and
...@@ -237,34 +235,7 @@ public final class SplittableRandom { ...@@ -237,34 +235,7 @@ public final class SplittableRandom {
s = (s << 8) | ((long)(seedBytes[i]) & 0xffL); s = (s << 8) | ((long)(seedBytes[i]) & 0xffL);
return s; return s;
} }
long h = 0L; return (mix64(System.currentTimeMillis()) ^
try {
Enumeration<NetworkInterface> ifcs =
NetworkInterface.getNetworkInterfaces();
boolean retry = false; // retry once if getHardwareAddress is null
while (ifcs.hasMoreElements()) {
NetworkInterface ifc = ifcs.nextElement();
if (!ifc.isVirtual()) { // skip fake addresses
byte[] bs = ifc.getHardwareAddress();
if (bs != null) {
int n = bs.length;
int m = Math.min(n >>> 1, 4);
for (int i = 0; i < m; ++i)
h = (h << 16) ^ (bs[i] << 8) ^ bs[n-1-i];
if (m < 4)
h = (h << 8) ^ bs[n-1-m];
h = mix64(h);
break;
}
else if (!retry)
retry = true;
else
break;
}
}
} catch (Exception ignore) {
}
return (h ^ mix64(System.currentTimeMillis()) ^
mix64(System.nanoTime())); mix64(System.nanoTime()));
} }
......
...@@ -36,8 +36,6 @@ ...@@ -36,8 +36,6 @@
package java.util.concurrent; package java.util.concurrent;
import java.io.ObjectStreamField; import java.io.ObjectStreamField;
import java.net.NetworkInterface;
import java.util.Enumeration;
import java.util.Random; import java.util.Random;
import java.util.Spliterator; import java.util.Spliterator;
import java.util.concurrent.atomic.AtomicInteger; import java.util.concurrent.atomic.AtomicInteger;
...@@ -147,34 +145,7 @@ public class ThreadLocalRandom extends Random { ...@@ -147,34 +145,7 @@ public class ThreadLocalRandom extends Random {
s = (s << 8) | ((long)(seedBytes[i]) & 0xffL); s = (s << 8) | ((long)(seedBytes[i]) & 0xffL);
return s; return s;
} }
long h = 0L; return (mix64(System.currentTimeMillis()) ^
try {
Enumeration<NetworkInterface> ifcs =
NetworkInterface.getNetworkInterfaces();
boolean retry = false; // retry once if getHardwareAddress is null
while (ifcs.hasMoreElements()) {
NetworkInterface ifc = ifcs.nextElement();
if (!ifc.isVirtual()) { // skip fake addresses
byte[] bs = ifc.getHardwareAddress();
if (bs != null) {
int n = bs.length;
int m = Math.min(n >>> 1, 4);
for (int i = 0; i < m; ++i)
h = (h << 16) ^ (bs[i] << 8) ^ bs[n-1-i];
if (m < 4)
h = (h << 8) ^ bs[n-1-m];
h = mix64(h);
break;
}
else if (!retry)
retry = true;
else
break;
}
}
} catch (Exception ignore) {
}
return (h ^ mix64(System.currentTimeMillis()) ^
mix64(System.nanoTime())); mix64(System.nanoTime()));
} }
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册