本文整理汇总了Java中java.util.concurrent.ArrayBlockingQueue.poll方法的典型用法代码示例。如果您正苦于以下问题:Java ArrayBlockingQueue.poll方法的具体用法?Java ArrayBlockingQueue.poll怎么用?Java ArrayBlockingQueue.poll使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类java.util.concurrent.ArrayBlockingQueue
的用法示例。
在下文中一共展示了ArrayBlockingQueue.poll方法的13个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: testDrainToN
import java.util.concurrent.ArrayBlockingQueue; //导入方法依赖的package包/类
/**
* drainTo(c, n) empties first min(n, size) elements of queue into c
*/
public void testDrainToN() {
ArrayBlockingQueue q = new ArrayBlockingQueue(SIZE * 2);
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);
}
}
示例2: levelOrderTraversal
import java.util.concurrent.ArrayBlockingQueue; //导入方法依赖的package包/类
public void levelOrderTraversal(Node node) {
if (node == null) {
return;
}
ArrayBlockingQueue<Node> queue = new ArrayBlockingQueue<>(10);
queue.offer(node);
while(!queue.isEmpty()) {
Node temp = queue.poll();
System.out.println(temp.getKey());
if (temp.leftNode != null) {
queue.offer(temp.leftNode);
}
if(temp.rightNode != null) {
queue.offer(temp.rightNode);
}
}
}
开发者ID:arunan123,项目名称:algorithm-implementation-in-java-javascript-scala,代码行数:20,代码来源:BinarySearchTreeMap.java
示例3: run
import java.util.concurrent.ArrayBlockingQueue; //导入方法依赖的package包/类
@Override
public void run() {
try {
final ByteArrayDiskQueue receivedURLs = frontier.receivedURLs;
final ArrayBlockingQueue<ByteArrayList> quickReceivedURLs = frontier.quickReceivedURLs;
while(! stop) {
final ByteArrayList list = quickReceivedURLs.poll(1, TimeUnit.SECONDS);
if (list != null) receivedURLs.enqueue(list.elements(), 0, list.size());
}
}
catch (Throwable t) {
LOGGER.error("Unexpected exception ", t);
}
LOGGER.info("Completed");
}
示例4: testDefaultThreadPool
import java.util.concurrent.ArrayBlockingQueue; //导入方法依赖的package包/类
@Test
public void testDefaultThreadPool() throws InterruptedException {
final ArrayBlockingQueue<Thread> sync = new ArrayBlockingQueue<>(1);
TaskExecutors.DEFAULT_THREAD_POOL.execute(
new Runnable() {
@Override
public void run() {
sync.add(Thread.currentThread());
}
});
Thread actual = sync.poll(TIMEOUT_MS, TimeUnit.MILLISECONDS);
Assert.assertNotEquals(Thread.currentThread(), actual);
}
示例5: testDirect
import java.util.concurrent.ArrayBlockingQueue; //导入方法依赖的package包/类
@Test
public void testDirect() throws InterruptedException {
final ArrayBlockingQueue<Thread> sync = new ArrayBlockingQueue<>(1);
TaskExecutors.DIRECT.execute(
new Runnable() {
@Override
public void run() {
sync.add(Thread.currentThread());
}
});
Thread actual = sync.poll(TIMEOUT_MS, TimeUnit.MILLISECONDS);
Assert.assertEquals(Thread.currentThread(), actual);
}
示例6: readPositionEnd
import java.util.concurrent.ArrayBlockingQueue; //导入方法依赖的package包/类
@Test
public void readPositionEnd() throws IOException, InterruptedException {
final Utils.LogFile logFile = new Utils.LogFile(100 * 1024, 400, 100);
logFile.close();
final ArrayBlockingQueue<FileChunk> chunkQueue = Queues.newArrayBlockingQueue(1);
final AsynchronousFileChannel channel = AsynchronousFileChannel.open(logFile.getPath(), StandardOpenOption.READ);
final CountingAsyncFileChannel spy = new CountingAsyncFileChannel(channel);
final ChunkReader chunkReader = new ChunkReader(mock(FileInput.class), logFile.getPath(), spy, chunkQueue, 10 * 1024,
FileInput.InitialReadPosition.END, null);
final ScheduledExecutorService chunkReaderExecutor = Executors.newSingleThreadScheduledExecutor(
new ThreadFactoryBuilder()
.setDaemon(false)
.setNameFormat("file-chunk-reader-%d")
.setUncaughtExceptionHandler(this)
.build()
);
final Thread consumer = new Thread() {
@Override
public void run() {
try {
final FileChunk chunk = chunkQueue.poll(2, TimeUnit.SECONDS);
assertNull("Reading from the end of the file must not produce a chunk for a non-changing file.", chunk);
} catch (InterruptedException ignore) {
}
}
};
consumer.start();
chunkReaderExecutor.scheduleAtFixedRate(chunkReader, 0, 250, TimeUnit.MILLISECONDS);
consumer.join();
// we can process one chunk at a time, so one read is queued, the second is buffered
assertEquals("The e should be empty", 1, chunkQueue.remainingCapacity());
}
示例7: call
import java.util.concurrent.ArrayBlockingQueue; //导入方法依赖的package包/类
@Override
public RpcResponse call() throws Exception {
//初始化返回结果容器,将本次调用的唯一标识作为Key存入返回结果的Map
RevokerResponseHolder.initResponseData(request.getUniqueKey());
//根据本地调用服务提供者地址获取对应的Netty通道channel队列
ArrayBlockingQueue<Channel> blockingQueue = NettyChannelPoolFactory.channelPoolFactoryInstance().acquire(inetSocketAddress);
try {
if (channel == null) {
//从队列中获取本次调用的Netty通道channel
channel = blockingQueue.poll(request.getInvokeTimeout(), TimeUnit.MILLISECONDS);
}
//若获取的channel通道已经不可用,则重新获取一个
while (!channel.isOpen() || !channel.isActive() || !channel.isWritable()) {
logger.warn("----------retry get new Channel------------");
channel = blockingQueue.poll(request.getInvokeTimeout(), TimeUnit.MILLISECONDS);
if (channel == null) {
//若队列中没有可用的Channel,则重新注册一个Channel
channel = NettyChannelPoolFactory.channelPoolFactoryInstance().registerChannel(inetSocketAddress);
}
}
//将本次调用的信息写入Netty通道,发起异步调用
ChannelFuture channelFuture = channel.writeAndFlush(request);
channelFuture.syncUninterruptibly();
//从返回结果容器中获取返回结果,同时设置等待超时时间为invokeTimeout
long invokeTimeout = request.getInvokeTimeout();
return RevokerResponseHolder.getValue(request.getUniqueKey(), invokeTimeout);
} catch (Exception e) {
logger.error("service invoke error.", e);
} finally {
//本次调用完毕后,将Netty的通道channel重新释放到队列中,以便下次调用复用
NettyChannelPoolFactory.channelPoolFactoryInstance().release(blockingQueue, channel, inetSocketAddress);
}
return null;
}
示例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++;
}
}
示例9: queueEmptyingWillClearItrs
import java.util.concurrent.ArrayBlockingQueue; //导入方法依赖的package包/类
public void queueEmptyingWillClearItrs() {
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++)
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);
}
for (int i = 0; i < capacity; i++)
q.poll();
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++;
}
}
示例10: 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++;
}
}
示例11: findMaxKeyBinaryTree
import java.util.concurrent.ArrayBlockingQueue; //导入方法依赖的package包/类
private K findMaxKeyBinaryTree(Node node) {
if (node == null)
return null;
K max = (K)node.key;
ArrayBlockingQueue<Node> queue = new ArrayBlockingQueue<>(20);
queue.offer(node);
while (!queue.isEmpty()) {
Node temp = queue.poll();
if(max.compareTo((K)temp.key) > 0) {
max = max;
} else {
max = (K) temp.key;
}
if (temp.leftNode != null) {
queue.offer(temp.leftNode);
}
if (temp.rightNode != null) {
queue.offer(temp.rightNode);
}
}
return max;
}
示例12: getGlobalTask
import java.util.concurrent.ArrayBlockingQueue; //导入方法依赖的package包/类
@Override
public GlobalTaskStartInfo getGlobalTask(ClusteredTask globalTask, long millis)
{
if( !globalTask.isGlobal() )
{
throw new Error("Must use startTask()");
}
final long end = System.currentTimeMillis() + millis;
final String globalId = globalTask.getGlobalId();
GlobalTaskStat globalTaskStat;
ArrayBlockingQueue<String> queue;
synchronized( stateMutex )
{
globalTaskStat = getOrAddGlobal(globalId);
if( globalTaskStat.getTaskId() != null )
{
return new GlobalTaskStartInfo(globalTaskStat.getTaskId(), true);
}
queue = new ArrayBlockingQueue<String>(1);
globalTaskStat.getWaiting().add(queue);
sendStartTask(globalTask);
}
try
{
String taskId = queue.poll(end - System.currentTimeMillis(), TimeUnit.MILLISECONDS);
synchronized( stateMutex )
{
globalTaskStat.getWaiting().remove(queue);
}
if( taskId != null )
{
return new GlobalTaskStartInfo(taskId, false);
}
throw new RuntimeException("Failed to start global task:" + globalId);
}
catch( InterruptedException e )
{
throw Throwables.propagate(e);
}
}
示例13: pollSendQueue
import java.util.concurrent.ArrayBlockingQueue; //导入方法依赖的package包/类
/**
* Retrieves and removes buffer at the head of this queue,
* waiting up to the specified wait time if necessary for an element to
* become available.
*
* {@link ArrayBlockingQueue#poll(long, java.util.concurrent.TimeUnit)}
*/
private ByteBuffer pollSendQueue(ArrayBlockingQueue<ByteBuffer> queue,
long timeout, TimeUnit unit) throws InterruptedException {
return queue.poll(timeout, unit);
}