jdk/src/share/classes/java/util/concurrent/ThreadLocalRandom.java
changeset 5773 f66c0faf0184
parent 5506 202f599c92aa
child 7518 0282db800fe1
equal deleted inserted replaced
5772:030ac5c2e62c 5773:f66c0faf0184
    71      * The random seed. We can't use super.seed.
    71      * The random seed. We can't use super.seed.
    72      */
    72      */
    73     private long rnd;
    73     private long rnd;
    74 
    74 
    75     /**
    75     /**
    76      * Initialization flag to permit the first and only allowed call
    76      * Initialization flag to permit calls to setSeed to succeed only
    77      * to setSeed (inside Random constructor) to succeed.  We can't
    77      * while executing the Random constructor.  We can't allow others
    78      * allow others since it would cause setting seed in one part of a
    78      * since it would cause setting seed in one part of a program to
    79      * program to unintentionally impact other usages by the thread.
    79      * unintentionally impact other usages by the thread.
    80      */
    80      */
    81     boolean initialized;
    81     boolean initialized;
    82 
    82 
    83     // Padding to help avoid memory contention among seed updates in
    83     // Padding to help avoid memory contention among seed updates in
    84     // different TLRs in the common case that they are located near
    84     // different TLRs in the common case that they are located near
    96     };
    96     };
    97 
    97 
    98 
    98 
    99     /**
    99     /**
   100      * Constructor called only by localRandom.initialValue.
   100      * Constructor called only by localRandom.initialValue.
   101      * We rely on the fact that the superclass no-arg constructor
       
   102      * invokes setSeed exactly once to initialize.
       
   103      */
   101      */
   104     ThreadLocalRandom() {
   102     ThreadLocalRandom() {
   105         super();
   103         super();
       
   104         initialized = true;
   106     }
   105     }
   107 
   106 
   108     /**
   107     /**
   109      * Returns the current thread's {@code ThreadLocalRandom}.
   108      * Returns the current thread's {@code ThreadLocalRandom}.
   110      *
   109      *
   121      * @throws UnsupportedOperationException always
   120      * @throws UnsupportedOperationException always
   122      */
   121      */
   123     public void setSeed(long seed) {
   122     public void setSeed(long seed) {
   124         if (initialized)
   123         if (initialized)
   125             throw new UnsupportedOperationException();
   124             throw new UnsupportedOperationException();
   126         initialized = true;
       
   127         rnd = (seed ^ multiplier) & mask;
   125         rnd = (seed ^ multiplier) & mask;
   128     }
   126     }
   129 
   127 
   130     protected int next(int bits) {
   128     protected int next(int bits) {
   131         rnd = (rnd * multiplier + addend) & mask;
   129         rnd = (rnd * multiplier + addend) & mask;