当前位置: 首页>>代码示例>>Java>>正文


Java ThreadLocalRandom.nextBoolean方法代码示例

本文整理汇总了Java中java.util.concurrent.ThreadLocalRandom.nextBoolean方法的典型用法代码示例。如果您正苦于以下问题:Java ThreadLocalRandom.nextBoolean方法的具体用法?Java ThreadLocalRandom.nextBoolean怎么用?Java ThreadLocalRandom.nextBoolean使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在java.util.concurrent.ThreadLocalRandom的用法示例。


在下文中一共展示了ThreadLocalRandom.nextBoolean方法的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: handleCoinToss

import java.util.concurrent.ThreadLocalRandom; //导入方法依赖的package包/类
private void handleCoinToss(@NotNull MessageReceivedEvent event, @NotNull List<String> args) {
    int iterations = 1;
    if (args.size() > 1 && INT_PATTERN.matcher(args.get(1)).find()) {
        iterations = Math.min(12, Integer.parseUnsignedInt(args.get(1)));
    }
    int sIterations = 0;
    ThreadLocalRandom random = ThreadLocalRandom.current();
    EmbedBuilder builder = MessageUtils.getEmbedBuilder(event.getMessage().getAuthor());
    int heads = 0;
    int tails = 0;
    while (sIterations++ != iterations) {
        boolean res = random.nextBoolean();

        if (res) heads++;
        else tails++;

        builder.appendField("Round " + (sIterations), res ? "Head" : "Tail", true);
    }
    builder.withTitle("Tossed " + (sIterations - 1) + (sIterations == 1 ? " coin" : " coins"));
    builder.withDescription("Heads: " + heads + ". Tails: " + tails);

    sendThenDelete(MessageUtils.getMessageBuilder(event.getMessage())
            .withEmbed(builder.build()));
    delete(event.getMessage());
}
 
开发者ID:ViniciusArnhold,项目名称:ProjectAltaria,代码行数:26,代码来源:RandomNumberCommand.java

示例2: testStickySpliteratorExhaustion

import java.util.concurrent.ThreadLocalRandom; //导入方法依赖的package包/类
/**
 * Concurrent Spliterators, once exhausted, stay exhausted.
 */
public void testStickySpliteratorExhaustion() throws Throwable {
    if (!impl.isConcurrent()) return;
    if (!testImplementationDetails) return;
    final ThreadLocalRandom rnd = ThreadLocalRandom.current();
    final Consumer alwaysThrows = e -> { throw new AssertionError(); };
    final Collection c = impl.emptyCollection();
    final Spliterator s = c.spliterator();
    if (rnd.nextBoolean()) {
        assertFalse(s.tryAdvance(alwaysThrows));
    } else {
        s.forEachRemaining(alwaysThrows);
    }
    final Object one = impl.makeElement(1);
    // Spliterator should not notice added element
    c.add(one);
    if (rnd.nextBoolean()) {
        assertFalse(s.tryAdvance(alwaysThrows));
    } else {
        s.forEachRemaining(alwaysThrows);
    }
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:25,代码来源:Collection8Test.java

示例3: randomColor

import java.util.concurrent.ThreadLocalRandom; //导入方法依赖的package包/类
private int randomColor() {
    ThreadLocalRandom random = ThreadLocalRandom.current();
    double rand = random.nextDouble(1, 100);

    if (rand <= 0.164) {
        return DyeColor.PINK.getWoolData();
    }

    if (rand <= 15) {
        return random.nextBoolean() ? DyeColor.BLACK.getWoolData() : random.nextBoolean() ? DyeColor.GRAY.getWoolData() : DyeColor.LIGHT_GRAY.getWoolData();
    }

    return DyeColor.WHITE.getWoolData();
}
 
开发者ID:Rsplwe,项目名称:Nukkit-Java9,代码行数:15,代码来源:EntitySheep.java

示例4: randomColor

import java.util.concurrent.ThreadLocalRandom; //导入方法依赖的package包/类
private int randomColor() {
    ThreadLocalRandom random = ThreadLocalRandom.current();
    double rand = random.nextDouble(1, 100);

    if (rand <= 0.164) {
        return DyeColor.PINK.getDyedData();
    }

    if (rand <= 15) {
        return random.nextBoolean() ? DyeColor.BLACK.getDyedData() : random.nextBoolean() ? DyeColor.GRAY.getDyedData() : DyeColor.LIGHT_GRAY.getDyedData();
    }

    return DyeColor.WHITE.getDyedData();
}
 
开发者ID:JupiterDevelopmentTeam,项目名称:Jupiter,代码行数:15,代码来源:EntitySheep.java

示例5: testElementRemovalDuringTraversal

import java.util.concurrent.ThreadLocalRandom; //导入方法依赖的package包/类
/**
 * All elements removed in the middle of CONCURRENT traversal.
 */
public void testElementRemovalDuringTraversal() {
    Collection c = impl.emptyCollection();
    ThreadLocalRandom rnd = ThreadLocalRandom.current();
    int n = rnd.nextInt(6);
    ArrayList copy = new ArrayList();
    for (int i = 0; i < n; i++) {
        Object x = impl.makeElement(i);
        copy.add(x);
        c.add(x);
    }
    ArrayList iterated = new ArrayList();
    ArrayList spliterated = new ArrayList();
    Spliterator s = c.spliterator();
    Iterator it = c.iterator();
    for (int i = rnd.nextInt(n + 1); --i >= 0; ) {
        assertTrue(s.tryAdvance(spliterated::add));
        if (rnd.nextBoolean()) assertTrue(it.hasNext());
        iterated.add(it.next());
    }
    Consumer alwaysThrows = e -> { throw new AssertionError(); };
    if (s.hasCharacteristics(Spliterator.CONCURRENT)) {
        c.clear();          // TODO: many more removal methods
        if (testImplementationDetails
            && !(c instanceof java.util.concurrent.ArrayBlockingQueue)) {
            if (rnd.nextBoolean())
                assertFalse(s.tryAdvance(alwaysThrows));
            else
                s.forEachRemaining(alwaysThrows);
        }
        if (it.hasNext()) iterated.add(it.next());
        if (rnd.nextBoolean()) assertIteratorExhausted(it);
    }
    assertTrue(copy.containsAll(iterated));
    assertTrue(copy.containsAll(spliterated));
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:39,代码来源:Collection8Test.java

示例6: randomOrder

import java.util.concurrent.ThreadLocalRandom; //导入方法依赖的package包/类
@SuppressWarnings("unchecked")
public static <T> Comparator<T> randomOrder() {
	ThreadLocalRandom r = ThreadLocalRandom.current();
	int x = r.nextInt(), y = r.nextInt();
	boolean b = r.nextBoolean();
	return Comparator.comparingInt((T t) -> t.hashCode() ^ x)
		.thenComparingInt(s -> s.toString().length() ^ y);
}
 
开发者ID:BRjDevs,项目名称:BRjLibs,代码行数:9,代码来源:CollectionUtils.java

示例7: populatedDeque

import java.util.concurrent.ThreadLocalRandom; //导入方法依赖的package包/类
/**
 * Returns a new deque of given size containing consecutive
 * Integers 0 ... n - 1.
 */
private static ArrayDeque<Integer> populatedDeque(int n) {
    // Randomize various aspects of memory layout, including
    // capacity slop and wraparound.
    final ArrayDeque<Integer> q;
    ThreadLocalRandom rnd = ThreadLocalRandom.current();
    switch (rnd.nextInt(6)) {
    case 0: q = new ArrayDeque<Integer>();      break;
    case 1: q = new ArrayDeque<Integer>(0);     break;
    case 2: q = new ArrayDeque<Integer>(1);     break;
    case 3: q = new ArrayDeque<Integer>(Math.max(0, n - 1)); break;
    case 4: q = new ArrayDeque<Integer>(n);     break;
    case 5: q = new ArrayDeque<Integer>(n + 1); break;
    default: throw new AssertionError();
    }
    switch (rnd.nextInt(3)) {
    case 0:
        q.addFirst(42);
        assertEquals((Integer) 42, q.removeLast());
        break;
    case 1:
        q.addLast(42);
        assertEquals((Integer) 42, q.removeFirst());
        break;
    case 2: /* do nothing */ break;
    default: throw new AssertionError();
    }
    assertTrue(q.isEmpty());
    if (rnd.nextBoolean())
        for (int i = 0; i < n; i++)
            assertTrue(q.offerLast((Integer) i));
    else
        for (int i = n; --i >= 0; )
            q.addFirst((Integer) i);
    assertEquals(n, q.size());
    if (n > 0) {
        assertFalse(q.isEmpty());
        assertEquals((Integer) 0, q.peekFirst());
        assertEquals((Integer) (n - 1), q.peekLast());
    }
    return q;
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:46,代码来源:ArrayDequeTest.java


注:本文中的java.util.concurrent.ThreadLocalRandom.nextBoolean方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。