1771N/A/*
1771N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
1771N/A *
1771N/A * This code is free software; you can redistribute it and/or modify it
1771N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation. Oracle designates this
1771N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
1771N/A *
1771N/A * This code is distributed in the hope that it will be useful, but WITHOUT
1771N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
1771N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
1771N/A * version 2 for more details (a copy is included in the LICENSE file that
1771N/A * accompanied this code).
1771N/A *
1771N/A * You should have received a copy of the GNU General Public License version
1771N/A * 2 along with this work; if not, write to the Free Software Foundation,
1771N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
1771N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
1771N/A */
1771N/A
1771N/A/*
1771N/A * This file is available under and governed by the GNU General Public
1771N/A * License version 2 only, as published by the Free Software Foundation.
1771N/A * However, the following notice accompanied the original version of this
1771N/A * file:
1771N/A *
1771N/A * Written by Doug Lea with assistance from members of JCP JSR-166
1771N/A * Expert Group and released to the public domain, as explained at
3984N/A * http://creativecommons.org/publicdomain/zero/1.0/
1771N/A */
1771N/A
1771N/Apackage java.util.concurrent;
1771N/A
1771N/Aimport java.util.Random;
1771N/A
1771N/A/**
1771N/A * A random number generator isolated to the current thread. Like the
1771N/A * global {@link java.util.Random} generator used by the {@link
1771N/A * java.lang.Math} class, a {@code ThreadLocalRandom} is initialized
1771N/A * with an internally generated seed that may not otherwise be
1771N/A * modified. When applicable, use of {@code ThreadLocalRandom} rather
1771N/A * than shared {@code Random} objects in concurrent programs will
1771N/A * typically encounter much less overhead and contention. Use of
1771N/A * {@code ThreadLocalRandom} is particularly appropriate when multiple
1771N/A * tasks (for example, each a {@link ForkJoinTask}) use random numbers
1771N/A * in parallel in thread pools.
1771N/A *
1771N/A * <p>Usages of this class should typically be of the form:
1771N/A * {@code ThreadLocalRandom.current().nextX(...)} (where
1771N/A * {@code X} is {@code Int}, {@code Long}, etc).
1771N/A * When all usages are of this form, it is never possible to
1771N/A * accidently share a {@code ThreadLocalRandom} across multiple threads.
1771N/A *
1771N/A * <p>This class also provides additional commonly used bounded random
1771N/A * generation methods.
1771N/A *
1771N/A * @since 1.7
1771N/A * @author Doug Lea
1771N/A */
1771N/Apublic class ThreadLocalRandom extends Random {
1771N/A // same constants as Random, but must be redeclared because private
3203N/A private static final long multiplier = 0x5DEECE66DL;
3203N/A private static final long addend = 0xBL;
3203N/A private static final long mask = (1L << 48) - 1;
1771N/A
1771N/A /**
1771N/A * The random seed. We can't use super.seed.
1771N/A */
1771N/A private long rnd;
1771N/A
1771N/A /**
2460N/A * Initialization flag to permit calls to setSeed to succeed only
2460N/A * while executing the Random constructor. We can't allow others
2460N/A * since it would cause setting seed in one part of a program to
2460N/A * unintentionally impact other usages by the thread.
1771N/A */
1771N/A boolean initialized;
1771N/A
1771N/A // Padding to help avoid memory contention among seed updates in
1771N/A // different TLRs in the common case that they are located near
1771N/A // each other.
1771N/A private long pad0, pad1, pad2, pad3, pad4, pad5, pad6, pad7;
1771N/A
1771N/A /**
1771N/A * The actual ThreadLocal
1771N/A */
1771N/A private static final ThreadLocal<ThreadLocalRandom> localRandom =
1771N/A new ThreadLocal<ThreadLocalRandom>() {
1771N/A protected ThreadLocalRandom initialValue() {
1771N/A return new ThreadLocalRandom();
1771N/A }
1771N/A };
1771N/A
1771N/A
1771N/A /**
1771N/A * Constructor called only by localRandom.initialValue.
1771N/A */
1771N/A ThreadLocalRandom() {
1771N/A super();
2460N/A initialized = true;
1771N/A }
1771N/A
1771N/A /**
1771N/A * Returns the current thread's {@code ThreadLocalRandom}.
1771N/A *
1771N/A * @return the current thread's {@code ThreadLocalRandom}
1771N/A */
1771N/A public static ThreadLocalRandom current() {
1771N/A return localRandom.get();
1771N/A }
1771N/A
1771N/A /**
1771N/A * Throws {@code UnsupportedOperationException}. Setting seeds in
1771N/A * this generator is not supported.
1771N/A *
1771N/A * @throws UnsupportedOperationException always
1771N/A */
1771N/A public void setSeed(long seed) {
1771N/A if (initialized)
1771N/A throw new UnsupportedOperationException();
1771N/A rnd = (seed ^ multiplier) & mask;
1771N/A }
1771N/A
1771N/A protected int next(int bits) {
1771N/A rnd = (rnd * multiplier + addend) & mask;
1771N/A return (int) (rnd >>> (48-bits));
1771N/A }
1771N/A
1771N/A /**
1771N/A * Returns a pseudorandom, uniformly distributed value between the
1771N/A * given least value (inclusive) and bound (exclusive).
1771N/A *
1771N/A * @param least the least value returned
1771N/A * @param bound the upper bound (exclusive)
1771N/A * @throws IllegalArgumentException if least greater than or equal
1771N/A * to bound
1771N/A * @return the next value
1771N/A */
1771N/A public int nextInt(int least, int bound) {
1771N/A if (least >= bound)
1771N/A throw new IllegalArgumentException();
1771N/A return nextInt(bound - least) + least;
1771N/A }
1771N/A
1771N/A /**
1771N/A * Returns a pseudorandom, uniformly distributed value
1771N/A * between 0 (inclusive) and the specified value (exclusive).
1771N/A *
1771N/A * @param n the bound on the random number to be returned. Must be
1771N/A * positive.
1771N/A * @return the next value
1771N/A * @throws IllegalArgumentException if n is not positive
1771N/A */
1771N/A public long nextLong(long n) {
1771N/A if (n <= 0)
1771N/A throw new IllegalArgumentException("n must be positive");
1771N/A // Divide n by two until small enough for nextInt. On each
1771N/A // iteration (at most 31 of them but usually much less),
1771N/A // randomly choose both whether to include high bit in result
1771N/A // (offset) and whether to continue with the lower vs upper
1771N/A // half (which makes a difference only if odd).
1771N/A long offset = 0;
1771N/A while (n >= Integer.MAX_VALUE) {
1771N/A int bits = next(2);
1771N/A long half = n >>> 1;
1771N/A long nextn = ((bits & 2) == 0) ? half : n - half;
1771N/A if ((bits & 1) == 0)
1771N/A offset += n - nextn;
1771N/A n = nextn;
1771N/A }
1771N/A return offset + nextInt((int) n);
1771N/A }
1771N/A
1771N/A /**
1771N/A * Returns a pseudorandom, uniformly distributed value between the
1771N/A * given least value (inclusive) and bound (exclusive).
1771N/A *
1771N/A * @param least the least value returned
1771N/A * @param bound the upper bound (exclusive)
1771N/A * @return the next value
1771N/A * @throws IllegalArgumentException if least greater than or equal
1771N/A * to bound
1771N/A */
1771N/A public long nextLong(long least, long bound) {
1771N/A if (least >= bound)
1771N/A throw new IllegalArgumentException();
1771N/A return nextLong(bound - least) + least;
1771N/A }
1771N/A
1771N/A /**
1771N/A * Returns a pseudorandom, uniformly distributed {@code double} value
1771N/A * between 0 (inclusive) and the specified value (exclusive).
1771N/A *
1771N/A * @param n the bound on the random number to be returned. Must be
1771N/A * positive.
1771N/A * @return the next value
1771N/A * @throws IllegalArgumentException if n is not positive
1771N/A */
1771N/A public double nextDouble(double n) {
1771N/A if (n <= 0)
1771N/A throw new IllegalArgumentException("n must be positive");
1771N/A return nextDouble() * n;
1771N/A }
1771N/A
1771N/A /**
1771N/A * Returns a pseudorandom, uniformly distributed value between the
1771N/A * given least value (inclusive) and bound (exclusive).
1771N/A *
1771N/A * @param least the least value returned
1771N/A * @param bound the upper bound (exclusive)
1771N/A * @return the next value
1771N/A * @throws IllegalArgumentException if least greater than or equal
1771N/A * to bound
1771N/A */
1771N/A public double nextDouble(double least, double bound) {
1771N/A if (least >= bound)
1771N/A throw new IllegalArgumentException();
1771N/A return nextDouble() * (bound - least) + least;
1771N/A }
1771N/A
1771N/A private static final long serialVersionUID = -5851777807851030925L;
1771N/A}