當前位置: 首頁>>代碼示例>>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;未經允許,請勿轉載。