Example usage for java.util.concurrent ThreadLocalRandom current

List of usage examples for java.util.concurrent ThreadLocalRandom current

Introduction

In this page you can find the example usage for java.util.concurrent ThreadLocalRandom current.

Prototype

public static ThreadLocalRandom current() 

Source Link

Document

Returns the current thread's ThreadLocalRandom .

Usage

From source file:dk.dma.navnet.server.AbstractServerConnectionTest.java

@Before
public void before() throws Exception {
    clientPort = ThreadLocalRandom.current().nextInt(40000, 50000);
    ServerConfiguration sc = new ServerConfiguration();
    sc.setServerPort(clientPort);//from   w  ww .  j  a va 2  s. c o m
    server = new InternalServer(sc);
    server.start();
}

From source file:org.apache.hadoop.hdfs.client.HdfsUtils.java

public static long calculateExponentialTime(long time, int retries, long cap) {
    long baseTime = Math.min(time * (1L << retries), cap);
    return (long) (baseTime * (ThreadLocalRandom.current().nextDouble() + 0.5));
}

From source file:net.maritimecloud.internal.client.AbstractRestTest.java

@Before
public void setup2() throws IOException {
    cliPort = ThreadLocalRandom.current().nextInt(30000, 40000);
    ic = new InternalClient(setupUpClient());
    ic.startBlocking();/*from w  w  w  . ja  v  a 2  s. com*/
}

From source file:cc.mallet.types.PolyaUrnDirichlet.java

protected long nextPoisson(double meanPoisson) {
    final double pivot = 40.0d;
    if (meanPoisson < pivot) {
        double p = FastMath.exp(-meanPoisson);
        long n = 0;
        double r = 1.0d;
        double rnd = 1.0d;

        while (n < 1000 * meanPoisson) {
            rnd = ThreadLocalRandom.current().nextDouble();
            r *= rnd;//from   w  w w .  j  a  v  a  2s  .  c o  m
            if (r >= p) {
                n++;
            } else {
                return n;
            }
        }
        return n;
    } else {
        final double lambda = FastMath.floor(meanPoisson);
        final double lambdaFractional = meanPoisson - lambda;
        final double logLambda = FastMath.log(lambda);
        final double logLambdaFactorial = CombinatoricsUtils.factorialLog((int) lambda);
        final long y2 = lambdaFractional < Double.MIN_VALUE ? 0 : nextPoisson(lambdaFractional);
        final double delta = FastMath.sqrt(lambda * FastMath.log(32 * lambda / FastMath.PI + 1));
        final double halfDelta = delta / 2;
        final double twolpd = 2 * lambda + delta;
        final double a1 = FastMath.sqrt(FastMath.PI * twolpd) * FastMath.exp(1 / (8 * lambda));
        final double a2 = (twolpd / delta) * FastMath.exp(-delta * (1 + delta) / twolpd);
        final double aSum = a1 + a2 + 1;
        final double p1 = a1 / aSum;
        final double p2 = a2 / aSum;
        final double c1 = 1 / (8 * lambda);

        double x = 0;
        double y = 0;
        double v = 0;
        int a = 0;
        double t = 0;
        double qr = 0;
        double qa = 0;
        for (;;) {
            final double u = ThreadLocalRandom.current().nextDouble();
            if (u <= p1) {
                final double n = ThreadLocalRandom.current().nextGaussian();
                x = n * FastMath.sqrt(lambda + halfDelta) - 0.5d;
                if (x > delta || x < -lambda) {
                    continue;
                }
                y = x < 0 ? FastMath.floor(x) : FastMath.ceil(x);
                final double e = nextStandardExponential();
                v = -e - (n * n / 2) + c1;
            } else {
                if (u > p1 + p2) {
                    y = lambda;
                    break;
                } else {
                    x = delta + (twolpd / delta) * nextStandardExponential();
                    y = FastMath.ceil(x);
                    v = -nextStandardExponential() - delta * (x + 1) / twolpd;
                }
            }
            a = x < 0 ? 1 : 0;
            t = y * (y + 1) / (2 * lambda);
            if (v < -t && a == 0) {
                y = lambda + y;
                break;
            }
            qr = t * ((2 * y + 1) / (6 * lambda) - 1);
            qa = qr - (t * t) / (3 * (lambda + a * (y + 1)));
            if (v < qa) {
                y = lambda + y;
                break;
            }
            if (v > qr) {
                continue;
            }
            if (v < y * logLambda - CombinatoricsUtils.factorialLog((int) (y + lambda)) + logLambdaFactorial) {
                y = lambda + y;
                break;
            }
        }
        return y2 + (long) y;
    }
}

From source file:com.haulmont.cuba.core.UniqueNumbersTest.java

@Test
public void testConcurrentModification() throws Exception {
    int threadCnt = 8;
    ExecutorService executorService = Executors.newFixedThreadPool(threadCnt,
            new ThreadFactoryBuilder().setNameFormat("T%d").build());

    final Action[] actions = { new SleepAction(), new GetNumberAction(), new SetNumberAction(),
            new DeleteSequenceAction() };

    for (int i = 0; i < threadCnt; i++) {
        final int finalI = i;
        executorService.submit(new Runnable() {

            int runnableNo = finalI;

            @Override/*w  ww.j  av  a  2  s.c o  m*/
            public void run() {
                ThreadLocalRandom random = ThreadLocalRandom.current();
                for (int i = 0; i < 10; i++) {
                    int action = random.nextInt(0, 4);
                    System.out.println(
                            "Runnable " + runnableNo + " iteration " + i + " action " + actions[action]);
                    try {
                        int seqN = actions[action].perform(runnableNo);
                        actions[action].success(runnableNo, seqN);
                    } catch (Exception e) {
                        if (e instanceof IllegalStateException
                                && StringUtils.contains(e.getMessage(), "Attempt to delete")) {
                            System.err.println(e.getMessage());
                            continue;
                        }
                        System.err.println(e.getMessage());
                        exceptionCnt.incrementAndGet();
                    }
                }
                finishedThreads.incrementAndGet();
            }
        });
    }

    while (finishedThreads.get() < threadCnt) {
        System.out.println("Waiting...");
        TimeUnit.MILLISECONDS.sleep(200);
    }

    assertEquals(exceptionCnt.get(), 0);
}

From source file:org.apache.druid.indexing.kafka.test.TestBroker.java

public void start() {
    final Properties props = new Properties();
    props.setProperty("zookeeper.connect", zookeeperConnect);
    props.setProperty("zookeeper.session.timeout.ms", "30000");
    props.setProperty("zookeeper.connection.timeout.ms", "30000");
    props.setProperty("log.dirs", directory.toString());
    props.setProperty("broker.id", String.valueOf(id));
    props.setProperty("port", String.valueOf(ThreadLocalRandom.current().nextInt(9999) + 10000));
    props.setProperty("advertised.host.name", "localhost");
    props.putAll(brokerProps);//from  w  ww  .  j  ava 2 s  .c  o m

    final KafkaConfig config = new KafkaConfig(props);

    server = new KafkaServer(config, SystemTime.SYSTEM,
            Some.apply(StringUtils.format("TestingBroker[%d]-", id)), List$.MODULE$.empty());
    server.startup();
}

From source file:com.google.cloud.bigtable.hbase.ManyThreadDriver.java

private static String key() {
    // TODO Make a parameter?
    return "key-" + ThreadLocalRandom.current().nextInt(100000);
}

From source file:nz.co.testamation.testcommon.fixture.SomeFixture.java

public static double someDouble() {
    return ThreadLocalRandom.current().nextDouble();
}

From source file:dpfmanager.shell.modules.server.core.ServerService.java

public int randInt(int min, int max) {
    return ThreadLocalRandom.current().nextInt((max - min) + 1) + min;
}

From source file:fi.luontola.cqrshotel.JsonSerializationTest.java

private static <T> T pickRandom(Collection<T> values) {
    ThreadLocalRandom random = ThreadLocalRandom.current();
    return values.stream().skip(random.nextInt(values.size())).findFirst().get();
}