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 |
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; |
136 |
|
do { |
137 |
|
oldseed = seed.get(); |
138 |
|
nextseed = (oldseed * multiplier + addend) & mask; |
139 |
< |
} while (!seed.compareAndSet(oldseed, nextseed)); |
139 |
> |
} while (!seed.weakCompareAndSet(oldseed, nextseed)); |
140 |
|
return (int)(nextseed >>> (48 - bits)); |
141 |
|
} |
142 |
|
|
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 |
|
} |