Home » openjdk-7 » java » util » concurrent » [javadoc | source]
java.util.concurrent
public class: ThreadLocalRandom [javadoc | source]
java.lang.Object
   java.util.Random
      java.util.concurrent.ThreadLocalRandom

All Implemented Interfaces:
    java$io$Serializable

A random number generator isolated to the current thread. Like the global java.util.Random generator used by the java.lang.Math class, a {@code ThreadLocalRandom} is initialized with an internally generated seed that may not otherwise be modified. When applicable, use of {@code ThreadLocalRandom} rather than shared {@code Random} objects in concurrent programs will typically encounter much less overhead and contention. Use of {@code 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: {@code ThreadLocalRandom.current().nextX(...)} (where {@code X} is {@code Int}, {@code Long}, etc). When all usages are of this form, it is never possible to accidently share a {@code ThreadLocalRandom} across multiple threads.

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

Field Summary
 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. 
Fields inherited from java.util.Random:
serialVersionUID
Constructor:
 ThreadLocalRandom() 
Method from java.util.concurrent.ThreadLocalRandom Summary:
current,   next,   nextDouble,   nextDouble,   nextInt,   nextLong,   nextLong,   setSeed
Methods from java.util.Random:
next,   nextBoolean,   nextBytes,   nextDouble,   nextFloat,   nextGaussian,   nextInt,   nextInt,   nextLong,   setSeed
Methods from java.lang.Object:
clone,   equals,   finalize,   getClass,   hashCode,   notify,   notifyAll,   toString,   wait,   wait,   wait
Method from java.util.concurrent.ThreadLocalRandom Detail:
 public static ThreadLocalRandom current() 
    Returns the current thread's {@code ThreadLocalRandom}.
 protected int next(int bits) 
 public double nextDouble(double n) 
    Returns a pseudorandom, uniformly distributed {@code double} value between 0 (inclusive) and the specified value (exclusive).
 public double nextDouble(double least,
    double bound) 
    Returns a pseudorandom, uniformly distributed value between the given least value (inclusive) and bound (exclusive).
 public int nextInt(int least,
    int bound) 
    Returns a pseudorandom, uniformly distributed value between the given least value (inclusive) and bound (exclusive).
 public long nextLong(long n) 
    Returns a pseudorandom, uniformly distributed value between 0 (inclusive) and the specified value (exclusive).
 public long nextLong(long least,
    long bound) 
    Returns a pseudorandom, uniformly distributed value between the given least value (inclusive) and bound (exclusive).
 public  void setSeed(long seed) 
    Throws {@code UnsupportedOperationException}. Setting seeds in this generator is not supported.