java.util.concurrent.BlockingQueue.drainTo()方法的使用及代码示例

x33g5p2x  于2022-01-16 转载在 其他  
字(6.0k)|赞(0)|评价(0)|浏览(246)

本文整理了Java中java.util.concurrent.BlockingQueue.drainTo()方法的一些代码示例,展示了BlockingQueue.drainTo()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。BlockingQueue.drainTo()方法的具体详情如下:
包路径:java.util.concurrent.BlockingQueue
类名称:BlockingQueue
方法名:drainTo

BlockingQueue.drainTo介绍

[英]Removes all available elements from this queue and adds them to the given collection. This operation may be more efficient than repeatedly polling this queue. A failure encountered while attempting to add elements to collection c may result in elements being in neither, either or both collections when the associated exception is thrown. Attempts to drain a queue to itself result in IllegalArgumentException. Further, the behavior of this operation is undefined if the specified collection is modified while the operation is in progress.
[中]从此队列中删除所有可用元素并将它们添加到给定集合。此操作可能比重复轮询此队列更有效。尝试将元素添加到集合c时遇到的故障可能会导致在引发关联异常时元素既不在集合中,也不在集合中或同时在集合中。尝试将队列排入自身会导致IllegalArgumentException。此外,如果在操作进行过程中修改了指定的集合,则此操作的行为是未定义的。

代码示例

代码示例来源:origin: google/guava

@Override
public int drainTo(Collection<? super E> c) {
 return delegate().drainTo(c);
}

代码示例来源:origin: google/guava

@Override
public int drainTo(Collection<? super E> c, int maxElements) {
 return delegate().drainTo(c, maxElements);
}

代码示例来源:origin: prestodb/presto

@Override
public int drainTo(Collection<? super E> c) {
 return delegate().drainTo(c);
}

代码示例来源:origin: prestodb/presto

@Override
public int drainTo(Collection<? super E> c, int maxElements) {
 return delegate().drainTo(c, maxElements);
}

代码示例来源:origin: MovingBlocks/Terasology

private void processPendingConnections() {
  if (!newClients.isEmpty()) {
    List<NetClient> newPlayers = Lists.newArrayListWithExpectedSize(newClients.size());
    newClients.drainTo(newPlayers);
    newPlayers.forEach(this::processNewClient);
  }
}

代码示例来源:origin: MovingBlocks/Terasology

private void processPendingDisconnects() {
  if (!disconnectedClients.isEmpty()) {
    List<NetClient> removedPlayers = Lists.newArrayListWithExpectedSize(disconnectedClients.size());
    disconnectedClients.drainTo(removedPlayers);
    removedPlayers.forEach(this::processRemovedClient);
  }
}

代码示例来源:origin: neo4j/neo4j

@Override
public void release()
{
  released = true;
  final List<MemoryBlock> blocks = new ArrayList<>();
  for ( final BlockingQueue<MemoryBlock> cache : caches )
  {
    cache.drainTo( blocks );
    blocks.forEach( block -> UnsafeUtil.free( block.unalignedAddr, block.unalignedSize ) );
    blocks.clear();
  }
}

代码示例来源:origin: google/j2objc

@Override
public int drainTo(Collection<? super E> c) {
 return delegate().drainTo(c);
}

代码示例来源:origin: google/j2objc

@Override
public int drainTo(Collection<? super E> c, int maxElements) {
 return delegate().drainTo(c, maxElements);
}

代码示例来源:origin: MovingBlocks/Terasology

@Override
public void update() {
  if (!reloadQueue.isEmpty()) {
    List<BlockTile> reloadList = Lists.newArrayListWithExpectedSize(reloadQueue.size());
    reloadQueue.drainTo(reloadList);
    // TODO: does this need to be more efficient? could just reload individual block tile locations.
    buildAtlas();
  }
}

代码示例来源:origin: MovingBlocks/Terasology

private void processReceivedChunks() {
  if (remoteWorldProvider != null) {
    List<Chunk> chunks = Lists.newArrayListWithExpectedSize(chunkQueue.size());
    chunkQueue.drainTo(chunks);
    for (Chunk chunk : chunks) {
      remoteWorldProvider.receiveChunk(chunk);
    }
  }
}

代码示例来源:origin: google/guava

added += q.drainTo(buffer, numElements - added);
if (added < numElements) { // not enough elements immediately available; will have to poll
 E e = q.poll(deadline - System.nanoTime(), TimeUnit.NANOSECONDS);

代码示例来源:origin: prestodb/presto

public void close()
{
  checkNotHoldsLock();
  List<PageReference> remainingPages = new ArrayList<>();
  SettableFuture<?> notEmptyFuture;
  synchronized (lock) {
    finishing = true;
    buffer.drainTo(remainingPages);
    bufferedBytes.addAndGet(-remainingPages.stream().mapToLong(PageReference::getRetainedSizeInBytes).sum());
    notEmptyFuture = this.notEmptyFuture;
    this.notEmptyFuture = NOT_EMPTY;
  }
  // free all the remaining pages
  remainingPages.forEach(PageReference::removePage);
  // notify readers outside of lock since this may result in a callback
  notEmptyFuture.set(null);
  // this will always fire the finished event
  checkState(isFinished(), "Expected buffer to be finished");
  checkFinished();
}

代码示例来源:origin: apache/incubator-druid

@Override
 public Boolean call()
 {
  while (!stopTest.get()) {
   q.drainTo(new ArrayList<>(), Integer.MAX_VALUE);
  }
  return true;
 }
}

代码示例来源:origin: MovingBlocks/Terasology

private void processReceivedMessages() {
  List<NetData.NetMessage> messages = Lists.newArrayListWithExpectedSize(queuedIncomingMessage.size());
  queuedIncomingMessage.drainTo(messages);
  for (NetData.NetMessage message : messages) {
    if (message.hasTime() && message.getTime() > lastReceivedTime) {
      lastReceivedTime = message.getTime();
    }
    processEntityUpdates(message);
    processEvents(message);
  }
}

代码示例来源:origin: MovingBlocks/Terasology

private void deactivateBlocks() {
  List<TShortObjectMap<TIntList>> deactivatedBlockSets = Lists.newArrayListWithExpectedSize(deactivateBlocksQueue.size());
  deactivateBlocksQueue.drainTo(deactivatedBlockSets);
  for (TShortObjectMap<TIntList> deactivatedBlockSet : deactivatedBlockSets) {
    deactivatedBlockSet.forEachEntry((id, positions) -> {
      if (positions.size() > 0) {
        blockManager.getBlock(id).getEntity().send(new BeforeDeactivateBlocks(positions, registry));
      }
      return true;
    });
  }
}

代码示例来源:origin: jankotek/mapdb

/**
 * drainTo(null) throws NullPointerException
 */
public void testDrainToNull() {
  final BlockingQueue q = emptyCollection();
  try {
    q.drainTo(null);
    shouldThrow();
  } catch (NullPointerException success) {}
}

代码示例来源:origin: jankotek/mapdb

/**
 * drainTo(this, n) throws IllegalArgumentException
 */
public void testDrainToSelfN() {
  final BlockingQueue q = emptyCollection();
  try {
    q.drainTo(q, 0);
    shouldThrow();
  } catch (IllegalArgumentException success) {}
}

代码示例来源:origin: jankotek/mapdb

/**
 * drainTo(this) throws IllegalArgumentException
 */
public void testDrainToSelf() {
  final BlockingQueue q = emptyCollection();
  try {
    q.drainTo(q);
    shouldThrow();
  } catch (IllegalArgumentException success) {}
}

代码示例来源:origin: jankotek/mapdb

/**
 * drainTo(null, n) throws NullPointerException
 */
public void testDrainToNullN() {
  final BlockingQueue q = emptyCollection();
  try {
    q.drainTo(null, 0);
    shouldThrow();
  } catch (NullPointerException success) {}
}

相关文章