Top Description Fields Constructors Methods
net.sf.ehcache.util.concurrent

public Class ThreadLocalRandom

extends Random
Class Inheritance
Imports
net.sf.ehcache.util.lang.VicariousThreadLocal, java.util.Random

A random number generator isolated to the current thread. Like the global java.util.Random generator used by the java.lang.Math class, a ThreadLocalRandom is initialized with an internally generated seed that may not otherwise be modified. When applicable, use of ThreadLocalRandom rather than shared Random objects in concurrent programs will typically encounter much less overhead and contention. Use of ThreadLocalRandom is particularly appropriate when multiple tasks (for example, each a ForkJoinTask) use random numbers in parallel in thread pools.

Usages of this class should typically be of the form: ThreadLocalRandom.current().nextX(...) (where X is Int, Long, etc). When all usages are of this form, it is never possible to accidently share a ThreadLocalRandom across multiple threads.

This class also provides additional commonly used bounded random generation methods.

Author
Doug Lea
Since
1.7

Field Summary

Modifier and TypeField and Description
private static final long
pack-priv boolean
initialized

Initialization flag to permit calls to setSeed to succeed only while executing the Random constructor.

private static final ThreadLocal<ThreadLocalRandom>
localRandom

The actual ThreadLocal

private static final long
private static final long
private long
private long
private long
private long
private long
private long
private long
private long
private long
rnd

The random seed.

private static final long

Constructor Summary

AccessConstructor and Description
pack-priv
ThreadLocalRandom()

Constructor called only by localRandom.initialValue.

Method Summary

Modifier and TypeMethod and Description
public static ThreadLocalRandom

Returns:

the current thread's ThreadLocalRandom
current
()

Returns the current thread's ThreadLocalRandom.

protected int
next(int
random bits
bits
)

Overrides java.util.Random.next.

Generates the next pseudorandom number.
public double

Returns:

the next value
nextDouble
(double
the bound on the random number to be returned. Must be positive.
n
)

Overrides default java.util.random.RandomGenerator.nextDouble.

Returns a pseudorandom, uniformly distributed double value between 0 (inclusive) and the specified value (exclusive).
public double

Returns:

the next value
nextDouble
(double
the least value returned
least
,
double
the upper bound (exclusive)
bound
)

Overrides default java.util.random.RandomGenerator.nextDouble.

Returns a pseudorandom, uniformly distributed value between the given least value (inclusive) and bound (exclusive).
public int

Returns:

the next value
nextInt
(int
the least value returned
least
,
int
the upper bound (exclusive)
bound
)

Overrides default java.util.random.RandomGenerator.nextInt.

Returns a pseudorandom, uniformly distributed value between the given least value (inclusive) and bound (exclusive).
public long

Returns:

the next value
nextLong
(long
the bound on the random number to be returned. Must be positive.
n
)

Overrides default java.util.random.RandomGenerator.nextLong.

Returns a pseudorandom, uniformly distributed value between 0 (inclusive) and the specified value (exclusive).
public long

Returns:

the next value
nextLong
(long
the least value returned
least
,
long
the upper bound (exclusive)
bound
)

Overrides default java.util.random.RandomGenerator.nextLong.

Returns a pseudorandom, uniformly distributed value between the given least value (inclusive) and bound (exclusive).
public void
setSeed(long
the seed value
seed
)

Overrides java.util.Random.setSeed.

Throws UnsupportedOperationException.
Inherited from java.util.Random:
doublesdoublesdoublesdoublesfromintsintsintsintslongslongslongslongsnextBooleannextBytesnextDoublenextFloatnextGaussiannextIntnextIntnextLong

Field Detail

addendback to summary
private static final long addend

Hides java.util.Random.addend.

initializedback to summary
pack-priv boolean initialized

Initialization flag to permit calls to setSeed to succeed only while executing the Random constructor. We can't allow others since it would cause setting seed in one part of a program to unintentionally impact other usages by the thread.

localRandomback to summary
private static final ThreadLocal<ThreadLocalRandom> localRandom

The actual ThreadLocal

maskback to summary
private static final long mask

Hides java.util.Random.mask.

multiplierback to summary
private static final long multiplier

Hides java.util.Random.multiplier.

pad0back to summary
private long pad0
pad1back to summary
private long pad1
pad2back to summary
private long pad2
pad3back to summary
private long pad3
pad4back to summary
private long pad4
pad5back to summary
private long pad5
pad6back to summary
private long pad6
pad7back to summary
private long pad7
rndback to summary
private long rnd

The random seed. We can't use super.seed.

serialVersionUIDback to summary
private static final long serialVersionUID

Hides java.util.Random.serialVersionUID.

Constructor Detail

ThreadLocalRandomback to summary
pack-priv ThreadLocalRandom()

Constructor called only by localRandom.initialValue.

Method Detail

currentback to summary
public static ThreadLocalRandom current()

Returns the current thread's ThreadLocalRandom.

Returns:ThreadLocalRandom

the current thread's ThreadLocalRandom

nextback to summary
protected int next(int bits)

Overrides java.util.Random.next.

Doc from java.util.Random.next.

Generates the next pseudorandom number. This method returns an int value such that, if the argument bits is between 1 and 32 (inclusive), then that many low-order bits of the returned value will be (approximately) independently chosen bit values, each of which is (approximately) equally likely to be 0 or 1.

Parameters
bits:int

random bits

Returns:int

the next pseudorandom value from this random number generator's sequence

nextDoubleback to summary
public double nextDouble(double n)

Overrides default java.util.random.RandomGenerator.nextDouble.

Returns a pseudorandom, uniformly distributed double value between 0 (inclusive) and the specified value (exclusive).

Parameters
n:double

the bound on the random number to be returned. Must be positive.

Returns:double

the next value

Exceptions
IllegalArgumentException:
if n is not positive
nextDoubleback to summary
public double nextDouble(double least, double bound)

Overrides default java.util.random.RandomGenerator.nextDouble.

Returns a pseudorandom, uniformly distributed value between the given least value (inclusive) and bound (exclusive).

Parameters
least:double

the least value returned

bound:double

the upper bound (exclusive)

Returns:double

the next value

Exceptions
IllegalArgumentException:
if least greater than or equal to bound
nextIntback to summary
public int nextInt(int least, int bound)

Overrides default java.util.random.RandomGenerator.nextInt.

Returns a pseudorandom, uniformly distributed value between the given least value (inclusive) and bound (exclusive).

Parameters
least:int

the least value returned

bound:int

the upper bound (exclusive)

Returns:int

the next value

Exceptions
IllegalArgumentException:
if least greater than or equal to bound
nextLongback to summary
public long nextLong(long n)

Overrides default java.util.random.RandomGenerator.nextLong.

Returns a pseudorandom, uniformly distributed value between 0 (inclusive) and the specified value (exclusive).

Parameters
n:long

the bound on the random number to be returned. Must be positive.

Returns:long

the next value

Exceptions
IllegalArgumentException:
if n is not positive
nextLongback to summary
public long nextLong(long least, long bound)

Overrides default java.util.random.RandomGenerator.nextLong.

Returns a pseudorandom, uniformly distributed value between the given least value (inclusive) and bound (exclusive).

Parameters
least:long

the least value returned

bound:long

the upper bound (exclusive)

Returns:long

the next value

Exceptions
IllegalArgumentException:
if least greater than or equal to bound
setSeedback to summary
public void setSeed(long seed)

Overrides java.util.Random.setSeed.

Throws UnsupportedOperationException. Setting seeds in this generator is not supported.

Parameters
seed:long

Doc from java.util.Random.setSeed.

the seed value

Exceptions
UnsupportedOperationException:
always