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