Java Code Examples for java.util.concurrent.ArrayBlockingQueue#iterator()

The following examples show how to use java.util.concurrent.ArrayBlockingQueue#iterator() . 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: ArrayBlockingQueueTest.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
/**
 * iterator iterates through all elements
 */
public void testIterator() throws InterruptedException {
    ArrayBlockingQueue q = populatedQueue(SIZE);
    Iterator it = q.iterator();
    int i;
    for (i = 0; it.hasNext(); i++)
        assertTrue(q.contains(it.next()));
    assertEquals(i, SIZE);
    assertIteratorExhausted(it);

    it = q.iterator();
    for (i = 0; it.hasNext(); i++)
        assertEquals(it.next(), q.take());
    assertEquals(i, SIZE);
    assertIteratorExhausted(it);
}
 
Example 2
Source File: ArrayBlockingQueueTest.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
/**
 * iterator.remove removes current element
 */
public void testIteratorRemove() {
    final ArrayBlockingQueue q = new ArrayBlockingQueue(3);
    q.add(two);
    q.add(one);
    q.add(three);

    Iterator it = q.iterator();
    it.next();
    it.remove();

    it = q.iterator();
    assertSame(it.next(), one);
    assertSame(it.next(), three);
    assertFalse(it.hasNext());
}
 
Example 3
Source File: WhiteBox.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Interior removal of elements used by an iterator will cause it
 * to be untracked.
 */
public void interiorRemovalOfElementsUsedByIterator() {
    boolean fair = rnd.nextBoolean();
    int capacity = rnd.nextInt(10, 20);
    ArrayBlockingQueue q = new ArrayBlockingQueue(capacity, fair);
    randomizePutIndex(q);
    q.add(0);
    for (int i = 1; i < 2 * capacity; i++) {
        q.add(i);
        Integer[] elts = { -1, -2, -3 };
        for (Integer elt : elts) q.add(elt);
        assertEquals(q.remove(), i - 1);
        Iterator it = q.iterator();
        assertEquals(it.next(), i);
        assertEquals(it.next(), elts[0]);
        Collections.shuffle(Arrays.asList(elts));
        assertTrue(q.remove(elts[0]));
        assertTrue(q.remove(elts[1]));
        assertEquals(trackedIterators(q), Collections.singletonList(it));
        assertTrue(q.remove(elts[2]));
        assertNull(itrs(q));
        assertEquals(it.next(), -2);
        assertIteratorExhausted(it);
        assertTrue(isDetached(it));
    }
}
 
Example 4
Source File: WhiteBox.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
public void garbageCollectionOfUnreachableIterators() {
    boolean fair = rnd.nextBoolean();
    int capacity = rnd.nextInt(1, 10);
    ArrayBlockingQueue q = new ArrayBlockingQueue(capacity, fair);
    randomizePutIndex(q);
    List<Iterator> its = new ArrayList<>();
    for (int i = 0; i < capacity; i++) q.add(i);
    for (int i = 0; i < capacity; i++) its.add(q.iterator());
    assertEquals(attachedIterators(q), its);
    its = null;
    gcAwait(() -> {
        List<Iterator> trackedIterators = trackedIterators(q);
        assertEquals(trackedIterators.size(), capacity);
        for (Iterator x : trackedIterators)
            if (x != null) return false;
        return true;
    });
    Iterator it = q.iterator(); //
    assertEquals(trackedIterators(q), Collections.singletonList(it));
}
 
Example 5
Source File: WhiteBox.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
public void iteratorsDetachedWhenExhaustedAndLastRetRemoved() {
    boolean fair = rnd.nextBoolean();
    int capacity = rnd.nextInt(2, 10);
    ArrayBlockingQueue q = new ArrayBlockingQueue(capacity, fair);
    randomizePutIndex(q);
    int size = rnd.nextInt(1, capacity + 1);
    for (int i = 0; i < size; i++) q.add(i);
    Iterator it = q.iterator();
    for (int i = 0; i < size - 1; i++) assertEquals(i, it.next());
    assertEquals(trackedIterators(q), Collections.singletonList(it));
    assertFalse(isDetached(it));
    switch (rnd.nextInt(2)) {
    case 0: assertTrue(q.remove(size - 1)); break;
    case 1: assertTrue(q.removeIf(e -> e.equals(size - 1))); break;
    default: throw new AssertionError();
    }
    assertEquals(size - 1, it.next()); // should trigger detach
    assertNull(itrs(q));
    assertTrue(isDetached(it));
    assertRemoveHasNoEffect(it, q);
}
 
Example 6
Source File: ArrayBlockingQueueTest.java    From j2objc with Apache License 2.0 6 votes vote down vote up
/**
 * iterator iterates through all elements
 */
public void testIterator() throws InterruptedException {
    ArrayBlockingQueue q = populatedQueue(SIZE);
    Iterator it = q.iterator();
    int i;
    for (i = 0; it.hasNext(); i++)
        assertTrue(q.contains(it.next()));
    assertEquals(i, SIZE);
    assertIteratorExhausted(it);

    it = q.iterator();
    for (i = 0; it.hasNext(); i++)
        assertEquals(it.next(), q.take());
    assertEquals(i, SIZE);
    assertIteratorExhausted(it);
}
 
Example 7
Source File: ArrayBlockingQueueTest.java    From j2objc with Apache License 2.0 6 votes vote down vote up
/**
 * iterator.remove removes current element
 */
public void testIteratorRemove() {
    final ArrayBlockingQueue q = new ArrayBlockingQueue(3);
    q.add(two);
    q.add(one);
    q.add(three);

    Iterator it = q.iterator();
    it.next();
    it.remove();

    it = q.iterator();
    assertSame(it.next(), one);
    assertSame(it.next(), three);
    assertFalse(it.hasNext());
}
 
Example 8
Source File: ArrayBlockingQueueTest.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * iterator ordering is FIFO
 */
public void testIteratorOrdering() {
    final ArrayBlockingQueue q = new ArrayBlockingQueue(3);
    q.add(one);
    q.add(two);
    q.add(three);

    assertEquals("queue should be full", 0, q.remainingCapacity());

    int k = 0;
    for (Iterator it = q.iterator(); it.hasNext();) {
        assertEquals(++k, it.next());
    }
    assertEquals(3, k);
}
 
Example 9
Source File: ArrayBlockingQueueTest.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Modifications do not cause iterators to fail
 */
public void testWeaklyConsistentIteration() {
    final ArrayBlockingQueue q = new ArrayBlockingQueue(3);
    q.add(one);
    q.add(two);
    q.add(three);
    for (Iterator it = q.iterator(); it.hasNext();) {
        q.remove();
        it.next();
    }
    assertEquals(0, q.size());
}
 
Example 10
Source File: WhiteBox.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
public void iteratorsOnEmptyQueue() {
    boolean fair = rnd.nextBoolean();
    int capacity = rnd.nextInt(1, 10);
    ArrayBlockingQueue q = new ArrayBlockingQueue(capacity, fair);
    randomizePutIndex(q);
    for (int i = 0; i < 4; i++) {
        Iterator it = q.iterator();
        assertNull(itrs(q));
        assertIteratorExhausted(it);
        assertTrue(isDetached(it));
        assertRemoveThrowsISE(it);
    }
}
 
Example 11
Source File: ArrayBlockingQueueTest.java    From j2objc with Apache License 2.0 5 votes vote down vote up
void checkToArray(ArrayBlockingQueue q) {
    int size = q.size();
    Object[] o = q.toArray();
    assertEquals(size, o.length);
    Iterator it = q.iterator();
    for (int i = 0; i < size; i++) {
        Integer x = (Integer) it.next();
        assertEquals((Integer)o[0] + i, (int) x);
        assertSame(o[i], x);
    }
}
 
Example 12
Source File: ArrayBlockingQueueTest.java    From j2objc with Apache License 2.0 5 votes vote down vote up
void checkToArray2(ArrayBlockingQueue q) {
    int size = q.size();
    Integer[] a1 = (size == 0) ? null : new Integer[size - 1];
    Integer[] a2 = new Integer[size];
    Integer[] a3 = new Integer[size + 2];
    if (size > 0) Arrays.fill(a1, 42);
    Arrays.fill(a2, 42);
    Arrays.fill(a3, 42);
    Integer[] b1 = (size == 0) ? null : (Integer[]) q.toArray(a1);
    Integer[] b2 = (Integer[]) q.toArray(a2);
    Integer[] b3 = (Integer[]) q.toArray(a3);
    assertSame(a2, b2);
    assertSame(a3, b3);
    Iterator it = q.iterator();
    for (int i = 0; i < size; i++) {
        Integer x = (Integer) it.next();
        assertSame(b1[i], x);
        assertEquals(b1[0] + i, (int) x);
        assertSame(b2[i], x);
        assertSame(b3[i], x);
    }
    assertNull(a3[size]);
    assertEquals(42, (int) a3[size + 1]);
    if (size > 0) {
        assertNotSame(a1, b1);
        assertEquals(size, b1.length);
        for (int i = 0; i < a1.length; i++) {
            assertEquals(42, (int) a1[i]);
        }
    }
}
 
Example 13
Source File: ArrayBlockingQueueTest.java    From j2objc with Apache License 2.0 5 votes vote down vote up
/**
 * iterator ordering is FIFO
 */
public void testIteratorOrdering() {
    final ArrayBlockingQueue q = new ArrayBlockingQueue(3);
    q.add(one);
    q.add(two);
    q.add(three);

    assertEquals("queue should be full", 0, q.remainingCapacity());

    int k = 0;
    for (Iterator it = q.iterator(); it.hasNext();) {
        assertEquals(++k, it.next());
    }
    assertEquals(3, k);
}
 
Example 14
Source File: ArrayBlockingQueueTest.java    From j2objc with Apache License 2.0 5 votes vote down vote up
/**
 * Modifications do not cause iterators to fail
 */
public void testWeaklyConsistentIteration() {
    final ArrayBlockingQueue q = new ArrayBlockingQueue(3);
    q.add(one);
    q.add(two);
    q.add(three);
    for (Iterator it = q.iterator(); it.hasNext();) {
        q.remove();
        it.next();
    }
    assertEquals(0, q.size());
}