ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/main/java/util/Random.java
(Generate patch)

Comparing jsr166/src/main/java/util/Random.java (file contents):
Revision 1.12 by jsr166, Sun Nov 6 07:47:51 2005 UTC vs.
Revision 1.19 by jsr166, Tue Jan 30 03:46:41 2007 UTC

# Line 1 | Line 1
1   /*
2   * %W% %E%
3   *
4 < * Copyright 2005 Sun Microsystems, Inc. All rights reserved.
4 > * Copyright 2007 Sun Microsystems, Inc. All rights reserved.
5   * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
6   */
7  
8   package java.util;
9   import java.io.*;
10   import java.util.concurrent.atomic.AtomicLong;
11 + import sun.misc.Unsafe;
12  
13   /**
14   * An instance of this class is used to generate a stream of
# Line 44 | Line 45 | class Random implements java.io.Serializ
45       * The internal state associated with this pseudorandom number generator.
46       * (The specs for the methods in this class describe the ongoing
47       * computation of this value.)
47     *
48     * @serial
48       */
49 <    private AtomicLong seed;
49 >    private final AtomicLong seed;
50  
51      private final static long multiplier = 0x5DEECE66DL;
52      private final static long addend = 0xBL;
# Line 464 | Line 463 | class Random implements java.io.Serializ
463      /**
464       * Serializable fields for Random.
465       *
466 <     * @serialField    seed long;
466 >     * @serialField    seed long
467       *              seed for random computations
468 <     * @serialField    nextNextGaussian double;
468 >     * @serialField    nextNextGaussian double
469       *              next Gaussian to be returned
470       * @serialField      haveNextNextGaussian boolean
471       *              nextNextGaussian is valid
# Line 492 | Line 491 | class Random implements java.io.Serializ
491          if (seedVal < 0)
492            throw new java.io.StreamCorruptedException(
493                                "Random: invalid seed");
494 <        seed = new AtomicLong(seedVal);
494 >        resetSeed(seedVal);
495          nextNextGaussian = fields.get("nextNextGaussian", 0.0);
496          haveNextNextGaussian = fields.get("haveNextNextGaussian", false);
497      }
# Line 515 | Line 514 | class Random implements java.io.Serializ
514          s.writeFields();
515      }
516  
517 +    // Support for resetting seed while deserializing
518 +    private static final Unsafe unsafe = Unsafe.getUnsafe();
519 +    private static final long seedOffset;
520 +    static {
521 +        try {
522 +            seedOffset = unsafe.objectFieldOffset
523 +                (Random.class.getDeclaredField("seed"));
524 +        } catch (Exception ex) { throw new Error(ex); }
525 +    }
526 +    private void resetSeed(long seedVal) {
527 +        unsafe.putObjectVolatile(this, seedOffset, new AtomicLong(seedVal));
528 +    }
529   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines