src/share/classes/java/util/concurrent/ThreadLocalRandom.java

Print this page




  46  * than shared {@code Random} objects in concurrent programs will
  47  * typically encounter much less overhead and contention.  Use of
  48  * {@code ThreadLocalRandom} is particularly appropriate when multiple
  49  * tasks (for example, each a {@link ForkJoinTask}) use random numbers
  50  * in parallel in thread pools.
  51  *
  52  * <p>Usages of this class should typically be of the form:
  53  * {@code ThreadLocalRandom.current().nextX(...)} (where
  54  * {@code X} is {@code Int}, {@code Long}, etc).
  55  * When all usages are of this form, it is never possible to
  56  * accidently share a {@code ThreadLocalRandom} across multiple threads.
  57  *
  58  * <p>This class also provides additional commonly used bounded random
  59  * generation methods.
  60  *
  61  * @since 1.7
  62  * @author Doug Lea
  63  */
  64 public class ThreadLocalRandom extends Random {
  65     // same constants as Random, but must be redeclared because private
  66     private final static long multiplier = 0x5DEECE66DL;
  67     private final static long addend = 0xBL;
  68     private final static long mask = (1L << 48) - 1;
  69 
  70     /**
  71      * The random seed. We can't use super.seed.
  72      */
  73     private long rnd;
  74 
  75     /**
  76      * Initialization flag to permit calls to setSeed to succeed only
  77      * while executing the Random constructor.  We can't allow others
  78      * since it would cause setting seed in one part of a program to
  79      * unintentionally impact other usages by the thread.
  80      */
  81     boolean initialized;
  82 
  83     // Padding to help avoid memory contention among seed updates in
  84     // different TLRs in the common case that they are located near
  85     // each other.
  86     private long pad0, pad1, pad2, pad3, pad4, pad5, pad6, pad7;
  87 
  88     /**




  46  * than shared {@code Random} objects in concurrent programs will
  47  * typically encounter much less overhead and contention.  Use of
  48  * {@code ThreadLocalRandom} is particularly appropriate when multiple
  49  * tasks (for example, each a {@link ForkJoinTask}) use random numbers
  50  * in parallel in thread pools.
  51  *
  52  * <p>Usages of this class should typically be of the form:
  53  * {@code ThreadLocalRandom.current().nextX(...)} (where
  54  * {@code X} is {@code Int}, {@code Long}, etc).
  55  * When all usages are of this form, it is never possible to
  56  * accidently share a {@code ThreadLocalRandom} across multiple threads.
  57  *
  58  * <p>This class also provides additional commonly used bounded random
  59  * generation methods.
  60  *
  61  * @since 1.7
  62  * @author Doug Lea
  63  */
  64 public class ThreadLocalRandom extends Random {
  65     // same constants as Random, but must be redeclared because private
  66     private static final long multiplier = 0x5DEECE66DL;
  67     private static final long addend = 0xBL;
  68     private static final long mask = (1L << 48) - 1;
  69 
  70     /**
  71      * The random seed. We can't use super.seed.
  72      */
  73     private long rnd;
  74 
  75     /**
  76      * Initialization flag to permit calls to setSeed to succeed only
  77      * while executing the Random constructor.  We can't allow others
  78      * since it would cause setting seed in one part of a program to
  79      * unintentionally impact other usages by the thread.
  80      */
  81     boolean initialized;
  82 
  83     // Padding to help avoid memory contention among seed updates in
  84     // different TLRs in the common case that they are located near
  85     // each other.
  86     private long pad0, pad1, pad2, pad3, pad4, pad5, pad6, pad7;
  87 
  88     /**