Java Code Examples for java.util.concurrent.LinkedTransferQueue#add()
The following examples show how to use
java.util.concurrent.LinkedTransferQueue#add() .
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: LinkedTransferQueueTest.java From j2objc with Apache License 2.0 | 6 votes |
/** * drainTo(c) empties queue into another collection c */ public void testDrainTo() { LinkedTransferQueue q = populatedQueue(SIZE); ArrayList l = new ArrayList(); q.drainTo(l); assertEquals(0, q.size()); assertEquals(SIZE, l.size()); for (int i = 0; i < SIZE; ++i) { assertEquals(i, l.get(i)); } q.add(zero); q.add(one); assertFalse(q.isEmpty()); assertTrue(q.contains(zero)); assertTrue(q.contains(one)); l.clear(); q.drainTo(l); assertEquals(0, q.size()); assertEquals(2, l.size()); for (int i = 0; i < 2; ++i) { assertEquals(i, l.get(i)); } }
Example 2
Source File: WhiteBox.java From openjdk-jdk9 with GNU General Public License v2.0 | 6 votes |
/** * Checks that traversal operations collapse a random pattern of * dead nodes as could normally only occur with a race. */ @Test(dataProvider = "traversalActions") public void traversalOperationsCollapseRandomNodes( Consumer<LinkedTransferQueue> traversalAction) { LinkedTransferQueue q = new LinkedTransferQueue(); int n = rnd.nextInt(6); for (int i = 0; i < n; i++) q.add(i); ArrayList nulledOut = new ArrayList(); for (Object p = head(q); p != null; p = next(p)) if (rnd.nextBoolean()) { nulledOut.add(item(p)); ITEM.setVolatile(p, null); } traversalAction.accept(q); int c = nodeCount(q); assertEquals(q.size(), c - (q.contains(n - 1) ? 0 : 1)); for (int i = 0; i < n; i++) assertTrue(nulledOut.contains(i) ^ q.contains(i)); }
Example 3
Source File: WhiteBox.java From openjdk-jdk9 with GNU General Public License v2.0 | 6 votes |
@Test(dataProvider = "pollActions") public void pollActionsOneNodeSlack( Consumer<LinkedTransferQueue> pollAction) { LinkedTransferQueue q = new LinkedTransferQueue(); int n = 1 + rnd.nextInt(5); for (int i = 0; i < n; i++) q.add(i); assertEquals(nodeCount(q), n + 1); for (int i = 0; i < n; i++) { int c = nodeCount(q); boolean slack = item(head(q)) == null; if (slack) assertNotNull(item(next(head(q)))); pollAction.accept(q); assertEquals(nodeCount(q), q.isEmpty() ? 1 : c - (slack ? 2 : 0)); } assertInvariants(q); }
Example 4
Source File: LinkedTransferQueueTest.java From j2objc with Apache License 2.0 | 6 votes |
/** * iterator.remove() removes current element */ public void testIteratorRemove() { final LinkedTransferQueue q = new LinkedTransferQueue(); 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 5
Source File: LinkedTransferQueueTest.java From openjdk-jdk9 with GNU General Public License v2.0 | 6 votes |
/** * iterator.remove() removes current element */ public void testIteratorRemove() { final LinkedTransferQueue q = new LinkedTransferQueue(); 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 6
Source File: LinkedTransferQueueTest.java From openjdk-jdk9 with GNU General Public License v2.0 | 6 votes |
/** * drainTo(c) empties queue into another collection c */ public void testDrainTo() { LinkedTransferQueue q = populatedQueue(SIZE); ArrayList l = new ArrayList(); q.drainTo(l); assertEquals(0, q.size()); assertEquals(SIZE, l.size()); for (int i = 0; i < SIZE; ++i) { assertEquals(i, l.get(i)); } q.add(zero); q.add(one); assertFalse(q.isEmpty()); assertTrue(q.contains(zero)); assertTrue(q.contains(one)); l.clear(); q.drainTo(l); assertEquals(0, q.size()); assertEquals(2, l.size()); for (int i = 0; i < 2; ++i) { assertEquals(i, l.get(i)); } }
Example 7
Source File: LinkedTransferQueueTest.java From openjdk-jdk9 with GNU General Public License v2.0 | 5 votes |
/** * iterator ordering is FIFO */ public void testIteratorOrdering() { final LinkedTransferQueue<Integer> q = new LinkedTransferQueue<>(); assertEquals(Integer.MAX_VALUE, q.remainingCapacity()); q.add(one); q.add(two); q.add(three); assertEquals(Integer.MAX_VALUE, q.remainingCapacity()); int k = 0; for (Integer n : q) { assertEquals(++k, (int) n); } assertEquals(3, k); }
Example 8
Source File: LinkedTransferQueueTest.java From j2objc with Apache License 2.0 | 5 votes |
/** * Modifications do not cause iterators to fail */ public void testWeaklyConsistentIteration() { final LinkedTransferQueue q = new LinkedTransferQueue(); 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 9
Source File: LinkedTransferQueueTest.java From j2objc with Apache License 2.0 | 5 votes |
/** * iterator ordering is FIFO */ public void testIteratorOrdering() { final LinkedTransferQueue<Integer> q = new LinkedTransferQueue<Integer>(); assertEquals(Integer.MAX_VALUE, q.remainingCapacity()); q.add(one); q.add(two); q.add(three); assertEquals(Integer.MAX_VALUE, q.remainingCapacity()); int k = 0; for (Integer n : q) { assertEquals(++k, (int) n); } assertEquals(3, k); }
Example 10
Source File: LinkedTransferQueueTest.java From j2objc with Apache License 2.0 | 5 votes |
/** * containsAll(c) is true when c contains a subset of elements */ public void testContainsAll() { LinkedTransferQueue<Integer> q = populatedQueue(SIZE); LinkedTransferQueue<Integer> p = new LinkedTransferQueue<Integer>(); for (int i = 0; i < SIZE; ++i) { assertTrue(q.containsAll(p)); assertFalse(p.containsAll(q)); p.add(i); } assertTrue(p.containsAll(q)); }
Example 11
Source File: LinkedTransferQueueTest.java From j2objc with Apache License 2.0 | 5 votes |
/** * clear removes all elements */ public void testClear() throws InterruptedException { LinkedTransferQueue q = populatedQueue(SIZE); q.clear(); checkEmpty(q); assertEquals(Integer.MAX_VALUE, q.remainingCapacity()); q.add(one); assertFalse(q.isEmpty()); assertEquals(1, q.size()); assertTrue(q.contains(one)); q.clear(); checkEmpty(q); }
Example 12
Source File: StorageMapPerfTest.java From Lealone-Plugins with Apache License 2.0 | 5 votes |
void testLinkedTransferQueue() { int count = 50000; long t1 = System.currentTimeMillis(); LinkedTransferQueue<String> tasks = new LinkedTransferQueue<>(); for (int i = 0; i < count; i++) { tasks.add("abc"); } long t2 = System.currentTimeMillis(); System.out.println("LinkedTransferQueue add time: " + (t2 - t1) + " ms, count: " + count); }
Example 13
Source File: LinkedTransferQueueTest.java From openjdk-jdk9 with GNU General Public License v2.0 | 5 votes |
/** * Modifications do not cause iterators to fail */ public void testWeaklyConsistentIteration() { final LinkedTransferQueue q = new LinkedTransferQueue(); 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 14
Source File: WhiteBox.java From openjdk-jdk9 with GNU General Public License v2.0 | 5 votes |
@Test public void addRemove() { LinkedTransferQueue q = new LinkedTransferQueue(); assertInvariants(q); assertNull(next(head(q))); assertNull(item(head(q))); q.add(1); assertEquals(nodeCount(q), 2); assertInvariants(q); q.remove(1); assertEquals(nodeCount(q), 1); assertInvariants(q); }
Example 15
Source File: LinkedTransferQueueTest.java From openjdk-jdk9 with GNU General Public License v2.0 | 5 votes |
/** * containsAll(c) is true when c contains a subset of elements */ public void testContainsAll() { LinkedTransferQueue<Integer> q = populatedQueue(SIZE); LinkedTransferQueue<Integer> p = new LinkedTransferQueue<>(); for (int i = 0; i < SIZE; ++i) { assertTrue(q.containsAll(p)); assertFalse(p.containsAll(q)); p.add(i); } assertTrue(p.containsAll(q)); }
Example 16
Source File: LinkedTransferQueueTest.java From openjdk-jdk9 with GNU General Public License v2.0 | 5 votes |
/** * clear removes all elements */ public void testClear() throws InterruptedException { LinkedTransferQueue q = populatedQueue(SIZE); q.clear(); checkEmpty(q); assertEquals(Integer.MAX_VALUE, q.remainingCapacity()); q.add(one); assertFalse(q.isEmpty()); assertEquals(1, q.size()); assertTrue(q.contains(one)); q.clear(); checkEmpty(q); }
Example 17
Source File: WhiteBox.java From openjdk-jdk9 with GNU General Public License v2.0 | 5 votes |
@Test(dataProvider = "bulkRemovalActions") public void bulkRemovalOperationsCollapseNodes( Consumer<LinkedTransferQueue> bulkRemovalAction) { LinkedTransferQueue q = new LinkedTransferQueue(); int n = 1 + rnd.nextInt(5); for (int i = 0; i < n; i++) q.add(i); bulkRemovalAction.accept(q); assertEquals(nodeCount(q), 1); assertInvariants(q); }
Example 18
Source File: WhiteBox.java From openjdk-jdk9 with GNU General Public License v2.0 | 5 votes |
@Test(dataProvider = "traversalActions") public void traversalOperationsCollapseLeadingNodes( Consumer<LinkedTransferQueue> traversalAction) { LinkedTransferQueue q = new LinkedTransferQueue(); Object oldHead; int n = 1 + rnd.nextInt(5); for (int i = 0; i < n; i++) q.add(i); assertEquals(nodeCount(q), n + 1); oldHead = head(q); traversalAction.accept(q); assertInvariants(q); assertEquals(nodeCount(q), n); assertIsSelfLinked(oldHead); }
Example 19
Source File: WhiteBox.java From openjdk-jdk9 with GNU General Public License v2.0 | 4 votes |
@Test(dataProvider = "traversalActions") public void traversalOperationsCollapseInteriorNodes( Consumer<LinkedTransferQueue> traversalAction) { LinkedTransferQueue q = new LinkedTransferQueue(); int n = 6; for (int i = 0; i < n; i++) q.add(i); // We must be quite devious to reliably create an interior dead node Object p0 = findNode(q, 0); Object p1 = findNode(q, 1); Object p2 = findNode(q, 2); Object p3 = findNode(q, 3); Object p4 = findNode(q, 4); Object p5 = findNode(q, 5); Iterator it1 = iteratorAt(q, 1); Iterator it2 = iteratorAt(q, 2); it2.remove(); // causes it2's ancestor to advance to 1 assertSame(next(p1), p3); assertSame(next(p2), p3); assertNull(item(p2)); it1.remove(); // removes it2's ancestor assertSame(next(p0), p3); assertSame(next(p1), p3); assertSame(next(p2), p3); assertNull(item(p1)); assertEquals(it2.next(), 3); it2.remove(); // it2's ancestor can't unlink assertSame(next(p0), p3); // p3 is now interior dead node assertSame(next(p1), p4); // it2 uselessly CASed p1.next assertSame(next(p2), p3); assertSame(next(p3), p4); assertInvariants(q); int c = nodeCount(q); traversalAction.accept(q); assertEquals(nodeCount(q), c - 1); assertSame(next(p0), p4); assertSame(next(p1), p4); assertSame(next(p2), p3); assertSame(next(p3), p4); assertInvariants(q); // trailing nodes are not unlinked Iterator it5 = iteratorAt(q, 5); it5.remove(); traversalAction.accept(q); assertSame(next(p4), p5); assertNull(next(p5)); assertEquals(nodeCount(q), c - 1); }