Java Code Examples for java.util.concurrent.SynchronousQueue#drainTo()

The following examples show how to use java.util.concurrent.SynchronousQueue#drainTo() . 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: SynchronousQueueTest.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
public void testDrainToWithActivePut(boolean fair) {
    final SynchronousQueue q = new SynchronousQueue(fair);
    Thread t = newStartedThread(new CheckedRunnable() {
        public void realRun() throws InterruptedException {
            q.put(one);
        }});

    ArrayList l = new ArrayList();
    long startTime = System.nanoTime();
    while (l.isEmpty()) {
        q.drainTo(l);
        if (millisElapsedSince(startTime) > LONG_DELAY_MS)
            fail("timed out");
        Thread.yield();
    }
    assertTrue(l.size() == 1);
    assertSame(one, l.get(0));
    awaitTermination(t);
}
 
Example 2
Source File: SynchronousQueueTest.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
/**
 * drainTo(c, n) empties up to n elements of queue into c
 */
public void testDrainToN() throws InterruptedException {
    final SynchronousQueue q = new SynchronousQueue();
    Thread t1 = newStartedThread(new CheckedRunnable() {
        public void realRun() throws InterruptedException {
            q.put(one);
        }});

    Thread t2 = newStartedThread(new CheckedRunnable() {
        public void realRun() throws InterruptedException {
            q.put(two);
        }});

    ArrayList l = new ArrayList();
    int drained;
    while ((drained = q.drainTo(l, 1)) == 0) Thread.yield();
    assertEquals(1, drained);
    assertEquals(1, l.size());
    while ((drained = q.drainTo(l, 1)) == 0) Thread.yield();
    assertEquals(1, drained);
    assertEquals(2, l.size());
    assertTrue(l.contains(one));
    assertTrue(l.contains(two));
    awaitTermination(t1);
    awaitTermination(t2);
}
 
Example 3
Source File: SynchronousQueueTest.java    From j2objc with Apache License 2.0 6 votes vote down vote up
public void testDrainToWithActivePut(boolean fair) {
    final SynchronousQueue q = new SynchronousQueue(fair);
    Thread t = newStartedThread(new CheckedRunnable() {
        public void realRun() throws InterruptedException {
            q.put(one);
        }});

    ArrayList l = new ArrayList();
    long startTime = System.nanoTime();
    while (l.isEmpty()) {
        q.drainTo(l);
        if (millisElapsedSince(startTime) > LONG_DELAY_MS)
            fail("timed out");
        Thread.yield();
    }
    assertTrue(l.size() == 1);
    assertSame(one, l.get(0));
    awaitTermination(t);
}
 
Example 4
Source File: SynchronousQueueTest.java    From j2objc with Apache License 2.0 6 votes vote down vote up
/**
 * drainTo(c, n) empties up to n elements of queue into c
 */
public void testDrainToN() throws InterruptedException {
    final SynchronousQueue q = new SynchronousQueue();
    Thread t1 = newStartedThread(new CheckedRunnable() {
        public void realRun() throws InterruptedException {
            q.put(one);
        }});

    Thread t2 = newStartedThread(new CheckedRunnable() {
        public void realRun() throws InterruptedException {
            q.put(two);
        }});

    ArrayList l = new ArrayList();
    int drained;
    while ((drained = q.drainTo(l, 1)) == 0) Thread.yield();
    assertEquals(1, drained);
    assertEquals(1, l.size());
    while ((drained = q.drainTo(l, 1)) == 0) Thread.yield();
    assertEquals(1, drained);
    assertEquals(2, l.size());
    assertTrue(l.contains(one));
    assertTrue(l.contains(two));
    awaitTermination(t1);
    awaitTermination(t2);
}
 
Example 5
Source File: SynchronousQueueTest.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
public void testDrainTo(boolean fair) {
    final SynchronousQueue q = new SynchronousQueue(fair);
    ArrayList l = new ArrayList();
    q.drainTo(l);
    assertEquals(0, q.size());
    assertEquals(0, l.size());
}
 
Example 6
Source File: SynchronousQueueTest.java    From j2objc with Apache License 2.0 5 votes vote down vote up
public void testDrainTo(boolean fair) {
    final SynchronousQueue q = new SynchronousQueue(fair);
    ArrayList l = new ArrayList();
    q.drainTo(l);
    assertEquals(0, q.size());
    assertEquals(0, l.size());
}