本文整理汇总了Java中java.util.concurrent.LinkedBlockingQueue.poll方法的典型用法代码示例。如果您正苦于以下问题:Java LinkedBlockingQueue.poll方法的具体用法?Java LinkedBlockingQueue.poll怎么用?Java LinkedBlockingQueue.poll使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类java.util.concurrent.LinkedBlockingQueue
的用法示例。
在下文中一共展示了LinkedBlockingQueue.poll方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: allocateCommBuffer
import java.util.concurrent.LinkedBlockingQueue; //导入方法依赖的package包/类
public static ByteBuffer allocateCommBuffer(int size, Socket sock) {
// I expect that size will almost always be the same value
if (sock.getChannel() == null) {
// The socket this commBuffer will be used for is old IO (it has no channel).
// So the commBuffer should be heap based.
return ByteBuffer.allocate(size);
}
LinkedBlockingQueue<ByteBuffer> q = commBufferMap.get(size);
ByteBuffer result = null;
if (q != null) {
result = q.poll();
}
if (result == null) {
result = ByteBuffer.allocateDirect(size);
} else {
result.position(0);
result.limit(result.capacity());
}
return result;
}
示例2: testDrainToN
import java.util.concurrent.LinkedBlockingQueue; //导入方法依赖的package包/类
/**
* drainTo(c, n) empties first min(n, size) elements of queue into c
*/
public void testDrainToN() {
LinkedBlockingQueue q = new LinkedBlockingQueue();
for (int i = 0; i < SIZE + 2; ++i) {
for (int j = 0; j < SIZE; j++)
assertTrue(q.offer(new Integer(j)));
ArrayList l = new ArrayList();
q.drainTo(l, i);
int k = (i < SIZE) ? i : SIZE;
assertEquals(k, l.size());
assertEquals(SIZE - k, q.size());
for (int j = 0; j < k; ++j)
assertEquals(l.get(j), new Integer(j));
do {} while (q.poll() != null);
}
}
示例3: testContains
import java.util.concurrent.LinkedBlockingQueue; //导入方法依赖的package包/类
/**
* contains(x) reports true when elements added but not yet removed
*/
public void testContains() {
LinkedBlockingQueue q = populatedQueue(SIZE);
for (int i = 0; i < SIZE; ++i) {
assertTrue(q.contains(new Integer(i)));
q.poll();
assertFalse(q.contains(new Integer(i)));
}
}
示例4: getAtMost
import java.util.concurrent.LinkedBlockingQueue; //导入方法依赖的package包/类
/**
* This removes the "num" values currently at the head of the Queue for the
* provided key. Will immediately fire the Queue filler function if key
* does not exist
* How many values are actually returned is governed by the
* <code>SyncGenerationPolicy</code> specified by the user.
* @param keyName String key name
* @param num Minimum number of values to return.
* @return List<E> values returned
* @throws IOException
* @throws ExecutionException
*/
public List<E> getAtMost(String keyName, int num) throws IOException,
ExecutionException {
LinkedBlockingQueue<E> keyQueue = keyQueues.get(keyName);
// Using poll to avoid race condition..
LinkedList<E> ekvs = new LinkedList<E>();
try {
for (int i = 0; i < num; i++) {
E val = keyQueue.poll();
// If queue is empty now, Based on the provided SyncGenerationPolicy,
// figure out how many new values need to be generated synchronously
if (val == null) {
// Synchronous call to get remaining values
int numToFill = 0;
switch (policy) {
case ATLEAST_ONE:
numToFill = (ekvs.size() < 1) ? 1 : 0;
break;
case LOW_WATERMARK:
numToFill =
Math.min(num, (int) (lowWatermark * numValues)) - ekvs.size();
break;
case ALL:
numToFill = num - ekvs.size();
break;
}
// Synchronous fill if not enough values found
if (numToFill > 0) {
refiller.fillQueueForKey(keyName, ekvs, numToFill);
}
// Asynch task to fill > lowWatermark
if (i <= (int) (lowWatermark * numValues)) {
submitRefillTask(keyName, keyQueue);
}
return ekvs;
}
ekvs.add(val);
}
} catch (Exception e) {
throw new IOException("Exeption while contacting value generator ", e);
}
return ekvs;
}