当前位置: 首页>>代码示例>>Java>>正文


Java ArrayBlockingQueue.add方法代码示例

本文整理汇总了Java中java.util.concurrent.ArrayBlockingQueue.add方法的典型用法代码示例。如果您正苦于以下问题:Java ArrayBlockingQueue.add方法的具体用法?Java ArrayBlockingQueue.add怎么用?Java ArrayBlockingQueue.add使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在java.util.concurrent.ArrayBlockingQueue的用法示例。


在下文中一共展示了ArrayBlockingQueue.add方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: addToSendQueue

import java.util.concurrent.ArrayBlockingQueue; //导入方法依赖的package包/类
/**
 * Inserts an element in the specified queue. If the Queue is full, this
 * method removes an element from the head of the Queue and then inserts
 * the element at the tail. It can happen that the an element is removed
 * by another thread in {@link SendWorker#processMessage() processMessage}
 * method before this method attempts to remove an element from the queue.
 * This will cause {@link ArrayBlockingQueue#remove() remove} to throw an
 * exception, which is safe to ignore.
 *
 * Unlike {@link #addToRecvQueue(Message) addToRecvQueue} this method does
 * not need to be synchronized since there is only one thread that inserts
 * an element in the queue and another thread that reads from the queue.
 *
 * @param queue
 *          Reference to the Queue
 * @param buffer
 *          Reference to the buffer to be inserted in the queue
 */
private void addToSendQueue(ArrayBlockingQueue<ByteBuffer> queue,
      ByteBuffer buffer) {
    if (queue.remainingCapacity() == 0) {
        try {
            queue.remove();
        } catch (NoSuchElementException ne) {
            // element could be removed by poll()
            LOG.debug("Trying to remove from an empty " +
                    "Queue. Ignoring exception " + ne);
        }
    }
    try {
        queue.add(buffer);
    } catch (IllegalStateException ie) {
        // This should never happen
        LOG.error("Unable to insert an element in the queue " + ie);
    }
}
 
开发者ID:didichuxing2,项目名称:https-github.com-apache-zookeeper,代码行数:37,代码来源:QuorumCnxManager.java

示例2: garbageCollectionOfUnreachableIterators

import java.util.concurrent.ArrayBlockingQueue; //导入方法依赖的package包/类
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));
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:21,代码来源:WhiteBox.java

示例3: testRun1

import java.util.concurrent.ArrayBlockingQueue; //导入方法依赖的package包/类
/**
 * Tests run() method.
 */
@Test
public void testRun1() throws Exception {
    blockingQueue = new ArrayBlockingQueue(5);
    channel = EasyMock.createMock(Channel.class);
    ospfArea = new OspfAreaImpl();
    lsaWrapper = new LsaWrapperImpl();
    routerLsa = new RouterLsa();
    routerLsa.setLsType(1);
    lsaWrapper.addLsa(OspfLsaType.ROUTER, routerLsa);
    ospfInterface = new OspfInterfaceImpl();
    ospfInterface.setState(OspfInterfaceState.DR);
    lsaWrapper.setOspfInterface(ospfInterface);
    lsaWrapper.setIsSelfOriginated(true);
    lsaHeader = new LsaHeader();
    lsaHeader.setLsType(1);
    lsaWrapper.setLsaHeader(lsaHeader);
    lsaWrapper.setLsaProcessing("refreshLsa");
    lsaWrapper.setLsdbAge(new LsdbAgeImpl(ospfArea));
    blockingQueue.add(lsaWrapper);
    lsaQueueConsumer = new LsaQueueConsumer(blockingQueue, channel, ospfArea);
    lsaQueueConsumer.run();
    assertThat(lsaQueueConsumer, is(notNullValue()));
}
 
开发者ID:shlee89,项目名称:athena,代码行数:27,代码来源:LsaQueueConsumerTest.java

示例4: testRun3

import java.util.concurrent.ArrayBlockingQueue; //导入方法依赖的package包/类
@Test
public void testRun3() throws Exception {
    blockingQueue = new ArrayBlockingQueue(5);
    channel = EasyMock.createMock(Channel.class);
    ospfArea = new OspfAreaImpl();
    lsaWrapper = new LsaWrapperImpl();
    routerLsa = new RouterLsa();
    routerLsa.setLsType(2);
    lsaWrapper.addLsa(OspfLsaType.NETWORK, routerLsa);
    ospfInterface = new OspfInterfaceImpl();
    ospfInterface.setState(OspfInterfaceState.BDR);
    lsaWrapper.setOspfInterface(ospfInterface);
    lsaWrapper.setIsSelfOriginated(true);
    lsaHeader = new LsaHeader();
    lsaHeader.setLsType(2);
    lsaWrapper.setLsaHeader(lsaHeader);
    lsaWrapper.setLsaProcessing("refreshLsa");
    lsaWrapper.setLsdbAge(new LsdbAgeImpl(ospfArea));
    blockingQueue.add(lsaWrapper);
    lsaQueueConsumer = new LsaQueueConsumer(blockingQueue, channel, ospfArea);
    lsaQueueConsumer.run();
    assertThat(lsaQueueConsumer, is(notNullValue()));
}
 
开发者ID:shlee89,项目名称:athena,代码行数:24,代码来源:LsaQueueConsumerTest.java

示例5: interiorRemovalOfElementsUsedByIterator

import java.util.concurrent.ArrayBlockingQueue; //导入方法依赖的package包/类
/**
 * 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));
    }
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:30,代码来源:WhiteBox.java

示例6: testToArray

import java.util.concurrent.ArrayBlockingQueue; //导入方法依赖的package包/类
/**
 * toArray() and toArray(a) contain all elements in FIFO order
 */
public void testToArray() {
    final ThreadLocalRandom rnd = ThreadLocalRandom.current();
    final int size = rnd.nextInt(6);
    final int capacity = Math.max(1, size + rnd.nextInt(size + 1));
    ArrayBlockingQueue<Integer> q = new ArrayBlockingQueue<>(capacity);
    for (int i = 0; i < size; i++) {
        checkToArray(q);
        q.add(i);
    }
    // Provoke wraparound
    int added = size * 2;
    for (int i = 0; i < added; i++) {
        checkToArray(q);
        assertEquals((Integer) i, q.poll());
        q.add(size + i);
    }
    for (int i = 0; i < size; i++) {
        checkToArray(q);
        assertEquals((Integer) (added + i), q.poll());
    }
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:25,代码来源:ArrayBlockingQueueTest.java

示例7: iteratorsDetachedWhenExhaustedAndLastRetRemoved

import java.util.concurrent.ArrayBlockingQueue; //导入方法依赖的package包/类
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);
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:22,代码来源:WhiteBox.java

示例8: clear_willClearItrs

import java.util.concurrent.ArrayBlockingQueue; //导入方法依赖的package包/类
public void clear_willClearItrs() {
    boolean fair = rnd.nextBoolean();
    int capacity = rnd.nextInt(2, 10);
    ArrayBlockingQueue q = new ArrayBlockingQueue(capacity, fair);
    randomizePutIndex(q);
    List<Iterator> its = new ArrayList<>();
    for (int i = 0; i < capacity; i++)
        assertTrue(q.add(i));
    assertNull(itrs(q));
    for (int i = 0; i < capacity; i++) {
        its.add(q.iterator());
        assertEquals(trackedIterators(q), its);
        q.poll();
        q.add(capacity + i);
    }
    q.clear();
    assertNull(itrs(q));
    int j = 0;
    for (Iterator it : its) {
        assertTrue(isDetached(it));
        if (rnd.nextBoolean()) assertTrue(it.hasNext());
        if (rnd.nextBoolean()) {
            assertEquals(it.next(), j);
            assertIteratorExhausted(it);
        }
        j++;
    }
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:29,代码来源:WhiteBox.java

示例9: testWeaklyConsistentIteration

import java.util.concurrent.ArrayBlockingQueue; //导入方法依赖的package包/类
/**
 * 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());
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:15,代码来源:ArrayBlockingQueueTest.java

示例10: testAddFail1

import java.util.concurrent.ArrayBlockingQueue; //导入方法依赖的package包/类
/**
 * This test tests the CheckedQueue.add method.  It creates a queue of
 * {@code String}s gets the checked queue, and attempt to add an Integer to
 * the checked queue.
 */
@Test(expectedExceptions = ClassCastException.class)
public void testAddFail1() {
    int arrayLength = 10;
    ArrayBlockingQueue<String> abq = new ArrayBlockingQueue(arrayLength + 1);

    for (int i = 0; i < arrayLength; i++) {
        abq.add(Integer.toString(i));
    }

    Queue q = Collections.checkedQueue(abq, String.class);
    q.add(0);
}
 
开发者ID:lambdalab-mirror,项目名称:jdk8u-jdk,代码行数:18,代码来源:CheckedQueue.java

示例11: advancing2CyclesWillRemoveIterators

import java.util.concurrent.ArrayBlockingQueue; //导入方法依赖的package包/类
public void advancing2CyclesWillRemoveIterators() {
    boolean fair = rnd.nextBoolean();
    int capacity = rnd.nextInt(2, 10);
    ArrayBlockingQueue q = new ArrayBlockingQueue(capacity, fair);
    List<Iterator> its = new ArrayList<>();
    for (int i = 0; i < capacity; i++)
        q.add(i);
    assertNull(itrs(q));
    for (int i = capacity; i < 3 * capacity; i++) {
        its.add(q.iterator());
        assertEquals(trackedIterators(q), its);
        q.poll();
        q.add(i);
    }
    for (int i = 3 * capacity; i < 4 * capacity; i++) {
        assertEquals(trackedIterators(q), its.subList(capacity,2*capacity));
        q.poll();
        q.add(i);
    }
    assertNull(itrs(q));
    int j = 0;
    for (Iterator it : its) {
        assertTrue(isDetached(it));
        if (rnd.nextBoolean()) assertTrue(it.hasNext());
        if (rnd.nextBoolean()) {
            assertEquals(it.next(), j);
            assertIteratorExhausted(it);
        }
        j++;
    }
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:32,代码来源:WhiteBox.java

示例12: collectiveSanity

import java.util.concurrent.ArrayBlockingQueue; //导入方法依赖的package包/类
/**
 * Checks collective sanity of iteration, toArray() and toString().
 */
public void collectiveSanity() {
    boolean fair = rnd.nextBoolean();
    int capacity = rnd.nextInt(10, 20);
    ArrayBlockingQueue q = new ArrayBlockingQueue(capacity, fair);
    randomizePutIndex(q);
    for (int i = 0; i < capacity; i++) {
        checkIterationSanity(q);
        assertEquals(capacity, q.size() + q.remainingCapacity());
        q.add(i);
    }
    for (int i = 0; i < (capacity + (capacity >> 1)); i++) {
        checkIterationSanity(q);
        assertEquals(capacity, q.size() + q.remainingCapacity());
        assertEquals(i, q.peek());
        assertEquals(i, q.poll());
        checkIterationSanity(q);
        assertEquals(capacity, q.size() + q.remainingCapacity());
        q.add(capacity + i);
    }
    for (int i = 0; i < capacity; i++) {
        checkIterationSanity(q);
        assertEquals(capacity, q.size() + q.remainingCapacity());
        int expected = i + capacity + (capacity >> 1);
        assertEquals(expected, q.peek());
        assertEquals(expected, q.poll());
    }
    checkIterationSanity(q);
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:32,代码来源:WhiteBox.java

示例13: testAdd

import java.util.concurrent.ArrayBlockingQueue; //导入方法依赖的package包/类
/**
 * add succeeds if not full; throws IllegalStateException if full
 */
public void testAdd() {
    ArrayBlockingQueue q = new ArrayBlockingQueue(SIZE);
    for (int i = 0; i < SIZE; i++) assertTrue(q.add((Integer) i));
    assertEquals(0, q.remainingCapacity());
    try {
        q.add((Integer) SIZE);
        shouldThrow();
    } catch (IllegalStateException success) {}
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:13,代码来源:ArrayBlockingQueueTest.java

示例14: testClear

import java.util.concurrent.ArrayBlockingQueue; //导入方法依赖的package包/类
/**
 * clear removes all elements
 */
public void testClear() {
    int size = ThreadLocalRandom.current().nextInt(1, 5);
    ArrayBlockingQueue q = populatedQueue(size, size, 2 * size, false);
    int capacity = size + q.remainingCapacity();
    q.clear();
    assertTrue(q.isEmpty());
    assertEquals(0, q.size());
    assertEquals(capacity, q.remainingCapacity());
    q.add(one);
    assertFalse(q.isEmpty());
    assertTrue(q.contains(one));
    q.clear();
    assertTrue(q.isEmpty());
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:18,代码来源:ArrayBlockingQueueTest.java

示例15: testContainsAll

import java.util.concurrent.ArrayBlockingQueue; //导入方法依赖的package包/类
/**
 * containsAll(c) is true when c contains a subset of elements
 */
public void testContainsAll() {
    ArrayBlockingQueue q = populatedQueue(SIZE);
    ArrayBlockingQueue p = new ArrayBlockingQueue(SIZE);
    for (int i = 0; i < SIZE; ++i) {
        assertTrue(q.containsAll(p));
        assertFalse(p.containsAll(q));
        p.add(new Integer(i));
    }
    assertTrue(p.containsAll(q));
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:14,代码来源:ArrayBlockingQueueTest.java


注:本文中的java.util.concurrent.ArrayBlockingQueue.add方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。