本文整理汇总了Java中java.util.concurrent.ArrayBlockingQueue.remainingCapacity方法的典型用法代码示例。如果您正苦于以下问题:Java ArrayBlockingQueue.remainingCapacity方法的具体用法?Java ArrayBlockingQueue.remainingCapacity怎么用?Java ArrayBlockingQueue.remainingCapacity使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类java.util.concurrent.ArrayBlockingQueue
的用法示例。
在下文中一共展示了ArrayBlockingQueue.remainingCapacity方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的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);
}
}
示例2: 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());
}
示例3: randomizePutIndex
import java.util.concurrent.ArrayBlockingQueue; //导入方法依赖的package包/类
/**
* Instead of having putIndex (and takeIndex) at the initial
* default of 0, move them to a random location.
*/
void randomizePutIndex(ArrayBlockingQueue q) {
assertTrue(q.isEmpty());
int capacity = q.remainingCapacity();
int n = rnd.nextInt(capacity + 1);
int putIndex = putIndex(q);
for (int i = n; i-->0; ) q.add(Boolean.TRUE);
for (int i = n; i-->0; ) q.remove();
assertEquals(putIndex(q), (putIndex + n) % items(q).length);
}