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 |
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; |
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 |
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 |
|
} |
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 |
|
} |