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.14 by dl, Thu Feb 2 20:09:07 2006 UTC

# Line 1 | Line 1
1   /*
2 < * %W% %E%
2 > * @(#)Random.java      1.46 05/11/30
3   *
4 < * Copyright 2005 Sun Microsystems, Inc. All rights reserved.
4 > * Copyright 2006 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 32 | Line 33 | import java.util.concurrent.atomic.Atomi
33   * Many applications will find the method {@link Math#random} simpler to use.
34   *
35   * @author  Frank Yellin
36 < * @version %I%, %G%
36 > * @version 1.46, 11/30/05
37   * @since   1.0
38   */
39   public
# Line 47 | Line 48 | class Random implements java.io.Serializ
48       *
49       * @serial
50       */
51 <    private AtomicLong seed;
51 >    private final AtomicLong seed;
52  
53      private final static long multiplier = 0x5DEECE66DL;
54      private final static long addend = 0xBL;
# Line 492 | Line 493 | class Random implements java.io.Serializ
493          if (seedVal < 0)
494            throw new java.io.StreamCorruptedException(
495                                "Random: invalid seed");
496 <        seed = new AtomicLong(seedVal);
496 >        resetSeed(seedVal);
497          nextNextGaussian = fields.get("nextNextGaussian", 0.0);
498          haveNextNextGaussian = fields.get("haveNextNextGaussian", false);
499      }
# Line 515 | Line 516 | class Random implements java.io.Serializ
516          s.writeFields();
517      }
518  
519 +    // Support for resetting seed while deserializing
520 +    private static final Unsafe unsafe = Unsafe.getUnsafe();
521 +    private static final long seedOffset;
522 +    static {
523 +        try {
524 +            seedOffset = unsafe.objectFieldOffset
525 +                (Random.class.getDeclaredField("seed"));
526 +            } catch (Exception ex) { throw new Error(ex); }
527 +    }
528 +    private void resetSeed(long seedVal) {
529 +        unsafe.putObjectVolatile(this, seedOffset, new AtomicLong(seedVal));
530 +    }
531 +
532   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines