[concurrency-interest] ThreadLocalRandom initial seed

Martin Buchholz martinrb at google.com
Mon Jun 6 16:27:44 EDT 2011


On Fri, Jun 3, 2011 at 14:35, Martin Buchholz <martinrb at google.com> wrote:

jsr166 CVS now has a fixed version of Random.java that causes
ThreadLocalRandom to work correctly.

This bug in openjdk7 Random.java is a regression from openjdk6 because
jsr166 CVS ThreadLocalRandom works just fine with openjdk6 Random, but
not with openjdk7 Random.java.  Similarily for any other Random
subclass that relied on the Random constructor calling setSeed, which
is a reasonable expectation given the javadoc.

http://download.oracle.com/javase/7/docs/api/java/util/Random.html#Random(long)

@@ -118,7 +118,13 @@
      * @see   #setSeed(long)
      */
     public Random(long seed) {
-        this.seed = new AtomicLong(initialScramble(seed));
+        if (getClass() == Random.class)
+            this.seed = new AtomicLong(initialScramble(seed));
+        else {
+            // subclass might have overriden setSeed
+            this.seed = new AtomicLong();
+            setSeed(seed);
+        }
     }

     private static long initialScramble(long seed) {


Martin

> diff --git a/src/share/classes/java/util/Random.java
> b/src/share/classes/java/util/Random.java
> --- a/src/share/classes/java/util/Random.java
> +++ b/src/share/classes/java/util/Random.java
> @@ -118,7 +118,13 @@
>      * @see   #setSeed(long)
>      */
>     public Random(long seed) {
> -        this.seed = new AtomicLong(initialScramble(seed));
> +        if (getClass() == Random.class)
> +            this.seed = new AtomicLong(initialScramble(seed));
> +        else {
> +            // subclass might have overriden setSeed
> +            this.seed = new AtomicLong(0L);
> +            setSeed(seed);
> +        }
>     }
>
>     private static long initialScramble(long seed) {
>



More information about the Concurrency-interest mailing list