[concurrency-interest] ThreadLocalRandom initial seed

David Holmes davidcholmes at aapt.net.au
Mon Jun 6 16:38:14 EDT 2011


Martin,

> 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.

This was the subject of the original bug and it was deemed that the spec did
not require that the constructor call setSeed - it only had to act, with
respect to the Random class itself, as-if it had called setSeed.

We've ended up with a bit of a mess here, but it's been discovered too late.

David
-----


> 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) {
>

_______________________________________________
Concurrency-interest mailing list
Concurrency-interest at cs.oswego.edu
http://cs.oswego.edu/mailman/listinfo/concurrency-interest




More information about the Concurrency-interest mailing list