Java Code Examples for java.util.concurrent.LinkedTransferQueue#take()

The following examples show how to use java.util.concurrent.LinkedTransferQueue#take() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: NameConstructors.java    From openjdk-jdk8u with GNU General Public License v2.0 6 votes vote down vote up
public static void test(Timer timer, String expected) throws InterruptedException {
    try {
        LinkedTransferQueue<String> queue = new LinkedTransferQueue<>();

        TimerTask task = new TimerTask() {
            public void run() {
                queue.put(Thread.currentThread().getName());
            }
        };

        timer.schedule(task, 0L); // immediately
        String actual = queue.take();

        if (!expected.equals(actual)) {
            throw new AssertionError(
                String.format("expected='%s', actual='%s'", expected, actual));
        }
    } finally {
        timer.cancel();
    }
}
 
Example 2
Source File: NameConstructors.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
public static void test(Timer timer, String expected) throws InterruptedException {
    try {
        LinkedTransferQueue<String> queue = new LinkedTransferQueue<>();

        TimerTask task = new TimerTask() {
            public void run() {
                queue.put(Thread.currentThread().getName());
            }
        };

        timer.schedule(task, 0L); // immediately
        String actual = queue.take();

        if (!expected.equals(actual)) {
            throw new AssertionError(
                String.format("expected='%s', actual='%s'", expected, actual));
        }
    } finally {
        timer.cancel();
    }
}
 
Example 3
Source File: NameConstructors.java    From jdk8u_jdk with GNU General Public License v2.0 6 votes vote down vote up
public static void test(Timer timer, String expected) throws InterruptedException {
    try {
        LinkedTransferQueue<String> queue = new LinkedTransferQueue<>();

        TimerTask task = new TimerTask() {
            public void run() {
                queue.put(Thread.currentThread().getName());
            }
        };

        timer.schedule(task, 0L); // immediately
        String actual = queue.take();

        if (!expected.equals(actual)) {
            throw new AssertionError(
                String.format("expected='%s', actual='%s'", expected, actual));
        }
    } finally {
        timer.cancel();
    }
}
 
Example 4
Source File: WhiteBox.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
public void cancelledNodeSweeping() throws Throwable {
    assertEquals(SWEEP_THRESHOLD & (SWEEP_THRESHOLD - 1), 0);
    LinkedTransferQueue q = new LinkedTransferQueue();
    Thread blockHead = null;
    if (rnd.nextBoolean()) {
        blockHead = new Thread(
            () -> { try { q.take(); } catch (InterruptedException ok) {}});
        blockHead.start();
        while (nodeCount(q) != 2) { Thread.yield(); }
        assertTrue(q.hasWaitingConsumer());
        assertEquals(q.getWaitingConsumerCount(), 1);
    }
    int initialNodeCount = nodeCount(q);

    // Some dead nodes do in fact accumulate ...
    if (blockHead != null)
        while (nodeCount(q) < initialNodeCount + SWEEP_THRESHOLD / 2)
            q.poll(1L, TimeUnit.MICROSECONDS);

    // ... but no more than SWEEP_THRESHOLD nodes accumulate
    for (int i = rnd.nextInt(SWEEP_THRESHOLD * 10); i-->0; )
        q.poll(1L, TimeUnit.MICROSECONDS);
    assertTrue(nodeCount(q) <= initialNodeCount + SWEEP_THRESHOLD);

    if (blockHead != null) {
        blockHead.interrupt();
        blockHead.join();
    }
}