本文整理汇总了Java中java.util.concurrent.BlockingQueue.drainTo方法的典型用法代码示例。如果您正苦于以下问题:Java BlockingQueue.drainTo方法的具体用法?Java BlockingQueue.drainTo怎么用?Java BlockingQueue.drainTo使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类java.util.concurrent.BlockingQueue
的用法示例。
在下文中一共展示了BlockingQueue.drainTo方法的14个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: enableDequeMode
import java.util.concurrent.BlockingQueue; //导入方法依赖的package包/类
/**
* enableDequeMode
*
* @param consumeBQueueSizeBeforeDeque
*/
protected void enableDequeMode(BlockingQueue<Runnable> workQueue) {
if (dequeEnableFlag.get() == false) {
synchronized (this) {
if (dequeEnableFlag.get() == false) {
// change deque enable flag=true
dequeEnableFlag.compareAndSet(false, true);
this.setName(threadCacheName + "-InDequeMode");
// we need drain out all tasks in BQueue
if (null != workQueue) {
List<Runnable> tasks = new ArrayList<Runnable>();
workQueue.drainTo(tasks);
workDeque.addAll(tasks);
}
}
}
}
}
示例2: testDelayQueue
import java.util.concurrent.BlockingQueue; //导入方法依赖的package包/类
void testDelayQueue(final BlockingQueue q) throws Throwable {
System.err.println(q.getClass().getSimpleName());
for (int i = 0; i < CAPACITY; i++)
q.add(new PDelay(i));
ArrayBlockingQueue q2 = new ArrayBlockingQueue(SMALL);
try {
q.drainTo(q2, SMALL + 3);
fail("should throw");
} catch (IllegalStateException success) {
equal(SMALL, q2.size());
equal(new PDelay(0), q2.poll());
equal(new PDelay(1), q2.poll());
check(q2.isEmpty());
for (int i = SMALL; i < CAPACITY; i++)
equal(new PDelay(i), q.poll());
equal(0, q.size());
}
}
示例3: putbackChunks
import java.util.concurrent.BlockingQueue; //导入方法依赖的package包/类
/**
* Add the chunks to the pool, when the pool achieves the max size, it will
* skip the remaining chunks
* @param chunks
*/
void putbackChunks(BlockingQueue<Chunk> chunks) {
int maxNumToPutback = this.maxCount - reclaimedChunks.size();
if (maxNumToPutback <= 0) {
return;
}
chunks.drainTo(reclaimedChunks, maxNumToPutback);
}
示例4: drainUninterruptibly
import java.util.concurrent.BlockingQueue; //导入方法依赖的package包/类
/**
* Drains the queue as {@linkplain #drain(BlockingQueue, Collection, int, long, TimeUnit)},
* but with a different behavior in case it is interrupted while waiting. In that case, the
* operation will continue as usual, and in the end the thread's interruption status will be set
* (no {@code InterruptedException} is thrown).
*
* @param q the blocking queue to be drained
* @param buffer where to add the transferred elements
* @param numElements the number of elements to be waited for
* @param timeout how long to wait before giving up, in units of {@code unit}
* @param unit a {@code TimeUnit} determining how to interpret the timeout parameter
* @return the number of elements transferred
*/
@Beta
public static <E> int drainUninterruptibly(BlockingQueue<E> q, Collection<? super E> buffer,
int numElements, long timeout, TimeUnit unit) {
Preconditions.checkNotNull(buffer);
long deadline = System.nanoTime() + unit.toNanos(timeout);
int added = 0;
boolean interrupted = false;
try {
while (added < numElements) {
// we could rely solely on #poll, but #drainTo might be more efficient when there are
// multiple elements already available (e.g. LinkedBlockingQueue#drainTo locks only once)
added += q.drainTo(buffer, numElements - added);
if (added < numElements) { // not enough elements immediately available; will have to poll
E e; // written exactly once, by a successful (uninterrupted) invocation of #poll
while (true) {
try {
e = q.poll(deadline - System.nanoTime(), TimeUnit.NANOSECONDS);
break;
} catch (InterruptedException ex) {
interrupted = true; // note interruption and retry
}
}
if (e == null) {
break; // we already waited enough, and there are no more elements in sight
}
buffer.add(e);
added++;
}
}
} finally {
if (interrupted) {
Thread.currentThread().interrupt();
}
}
return added;
}
示例5: drainTo
import java.util.concurrent.BlockingQueue; //导入方法依赖的package包/类
/**
* drainTo defers to each sub-queue. Note that draining from a FairCallQueue
* to another FairCallQueue will likely fail, since the incoming calls
* may be scheduled differently in the new FairCallQueue. Nonetheless this
* method is provided for completeness.
*/
@Override
public int drainTo(Collection<? super E> c, int maxElements) {
int sum = 0;
for (BlockingQueue<E> q : this.queues) {
sum += q.drainTo(c, maxElements);
}
return sum;
}
示例6: getRAMQueueEntries
import java.util.concurrent.BlockingQueue; //导入方法依赖的package包/类
/**
* Blocks until elements available in <code>q</code> then tries to grab as many as possible
* before returning.
* @param recepticle Where to stash the elements taken from queue. We clear before we use it
* just in case.
* @param q The queue to take from.
* @return <code>receptical laden with elements taken from the queue or empty if none found.
*/
@VisibleForTesting
static List<RAMQueueEntry> getRAMQueueEntries(final BlockingQueue<RAMQueueEntry> q,
final List<RAMQueueEntry> receptical)
throws InterruptedException {
// Clear sets all entries to null and sets size to 0. We retain allocations. Presume it
// ok even if list grew to accommodate thousands.
receptical.clear();
receptical.add(q.take());
q.drainTo(receptical);
return receptical;
}
示例7: drain
import java.util.concurrent.BlockingQueue; //导入方法依赖的package包/类
/**
* Drains the queue as {@link BlockingQueue#drainTo(Collection, int)}, but if the requested
* {@code numElements} elements are not available, it will wait for them up to the specified
* timeout.
*
* @param q the blocking queue to be drained
* @param buffer where to add the transferred elements
* @param numElements the number of elements to be waited for
* @param timeout how long to wait before giving up, in units of {@code unit}
* @param unit a {@code TimeUnit} determining how to interpret the timeout parameter
* @return the number of elements transferred
* @throws InterruptedException if interrupted while waiting
*/
@Beta
@CanIgnoreReturnValue
@GwtIncompatible // BlockingQueue
public static <E> int drain(
BlockingQueue<E> q,
Collection<? super E> buffer,
int numElements,
long timeout,
TimeUnit unit)
throws InterruptedException {
Preconditions.checkNotNull(buffer);
/*
* This code performs one System.nanoTime() more than necessary, and in return, the time to
* execute Queue#drainTo is not added *on top* of waiting for the timeout (which could make
* the timeout arbitrarily inaccurate, given a queue that is slow to drain).
*/
long deadline = System.nanoTime() + unit.toNanos(timeout);
int added = 0;
while (added < numElements) {
// we could rely solely on #poll, but #drainTo might be more efficient when there are multiple
// elements already available (e.g. LinkedBlockingQueue#drainTo locks only once)
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);
if (e == null) {
break; // we already waited enough, and there are no more elements in sight
}
buffer.add(e);
added++;
}
}
return added;
}
示例8: drain
import java.util.concurrent.BlockingQueue; //导入方法依赖的package包/类
/**
* Drains the queue as {@link BlockingQueue#drainTo(Collection, int)}, but if the requested
* {@code numElements} elements are not available, it will wait for them up to the specified
* timeout.
*
* @param q the blocking queue to be drained
* @param buffer where to add the transferred elements
* @param numElements the number of elements to be waited for
* @param timeout how long to wait before giving up, in units of {@code unit}
* @param unit a {@code TimeUnit} determining how to interpret the timeout parameter
* @return the number of elements transferred
* @throws InterruptedException if interrupted while waiting
*/
@Beta
public static <E> int drain(BlockingQueue<E> q, Collection<? super E> buffer, int numElements,
long timeout, TimeUnit unit) throws InterruptedException {
Preconditions.checkNotNull(buffer);
/*
* This code performs one System.nanoTime() more than necessary, and in return, the time to
* execute Queue#drainTo is not added *on top* of waiting for the timeout (which could make
* the timeout arbitrarily inaccurate, given a queue that is slow to drain).
*/
long deadline = System.nanoTime() + unit.toNanos(timeout);
int added = 0;
while (added < numElements) {
// we could rely solely on #poll, but #drainTo might be more efficient when there are multiple
// elements already available (e.g. LinkedBlockingQueue#drainTo locks only once)
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);
if (e == null) {
break; // we already waited enough, and there are no more elements in sight
}
buffer.add(e);
added++;
}
}
return added;
}
示例9: pumpData
import java.util.concurrent.BlockingQueue; //导入方法依赖的package包/类
/**
* Pumps data from the queue, within the agent context.
*
* @param apmAgentContext the agent context
* @param queue the data queue
*/
protected void pumpData (ApmAgentContext apmAgentContext, BlockingQueue<T> queue) throws IOException {
final List<T> data = new ArrayList<> ();
queue.drainTo (data, apmAgentContext.getPumpBatchSize ());
if (data.isEmpty ()) {
logger.info ("No data to pump");
} else {
logger.info ("Pumping data");
sendData (apmAgentContext, data);
}
}
示例10: testDrainToSelfN
import java.util.concurrent.BlockingQueue; //导入方法依赖的package包/类
/**
* drainTo(this, n) throws IllegalArgumentException
*/
public void testDrainToSelfN() {
final BlockingQueue q = emptyCollection();
try {
q.drainTo(q, 0);
shouldThrow();
} catch (IllegalArgumentException success) {}
}
示例11: drainQueue
import java.util.concurrent.BlockingQueue; //导入方法依赖的package包/类
/**
* Drains the task queue into a new list, normally using drainTo. But if the queue is a DelayQueue or any other kind
* of queue for which poll or drainTo may fail to remove some elements, it deletes them one by one.
*/
private List<Runnable> drainQueue() {
BlockingQueue<Runnable> q = workQueue;
List<Runnable> taskList = new ArrayList<Runnable>();
q.drainTo(taskList);
if (!q.isEmpty()) {
for (Runnable r : q.toArray(new Runnable[0])) {
if (q.remove(r))
taskList.add(r);
}
}
return taskList;
}
示例12: testDrainToNull
import java.util.concurrent.BlockingQueue; //导入方法依赖的package包/类
/**
* drainTo(null) throws NullPointerException
*/
public void testDrainToNull() {
final BlockingQueue q = emptyCollection();
try {
q.drainTo(null);
shouldThrow();
} catch (NullPointerException success) {}
}
示例13: stream
import java.util.concurrent.BlockingQueue; //导入方法依赖的package包/类
public Stream<TweetHandler> stream() {
String myKey = "sl2WbCf4UnIr08xvHVitHJ99r";
String mySecret = "PE6yauvXjKLuvoQNXZAJo5C8N5U5piSFb3udwkoI76paK6KyqI";
String myToken = "1098376471-p6iWfxCLtyMvMutTb010w1D1xZ3UyJhcC2kkBjN";
String myAccess = "2o1uGcp4b2bFynOfu2cA1uz63n5aruV0RwNsUjRpjDBZS";
out.println("Creating Twitter Stream");
BlockingQueue<String> statusQueue = new LinkedBlockingQueue<>(1000);
StatusesFilterEndpoint endpoint = new StatusesFilterEndpoint();
endpoint.trackTerms(Lists.newArrayList("twitterapi", this.topic));
endpoint.stallWarnings(false);
Authentication twitterAuth = new OAuth1(myKey, mySecret, myToken, myAccess);
BasicClient twitterClient = new ClientBuilder()
.name("Twitter client")
.hosts(Constants.STREAM_HOST)
.endpoint(endpoint)
.authentication(twitterAuth)
.processor(new StringDelimitedProcessor(statusQueue))
.build();
twitterClient.connect();
List<TweetHandler> list = new ArrayList();
List<String> twitterList = new ArrayList();
statusQueue.drainTo(twitterList);
for(int i=0; i<numberOfTweets; i++) {
String message;
try {
message = statusQueue.take();
list.add(new TweetHandler(message));
} catch (InterruptedException ex) {
ex.printStackTrace();
}
}
// for (int msgRead = 0; msgRead < this.numberOfTweets; msgRead++) {
// try {
// if (twitterClient.isDone()) {
// // out.println(twitterClient.getExitEvent().getMessage());
// break;
// }
//
// String msg = statusQueue.poll(10, TimeUnit.SECONDS);
// if (msg == null) {
// out.println("Waited 10 seconds - no message received");
// } else {
// list.add(new TweetHandler(msg));
// out.println("Added message: " + msg.length());
// }
// } catch (InterruptedException ex) {
// ex.printStackTrace();
// }
// }
twitterClient.stop();
out.printf("%d messages processed!\n", twitterClient.getStatsTracker().getNumMessages());
return list.stream();
}
开发者ID:PacktPublishing,项目名称:Machine-Learning-End-to-Endguide-for-Java-developers,代码行数:61,代码来源:TwitterStream.java
示例14: drainUninterruptibly
import java.util.concurrent.BlockingQueue; //导入方法依赖的package包/类
/**
* Drains the queue as {@linkplain #drain(BlockingQueue, Collection, int, long, TimeUnit)},
* but with a different behavior in case it is interrupted while waiting. In that case, the
* operation will continue as usual, and in the end the thread's interruption status will be set
* (no {@code InterruptedException} is thrown).
*
* @param q the blocking queue to be drained
* @param buffer where to add the transferred elements
* @param numElements the number of elements to be waited for
* @param timeout how long to wait before giving up, in units of {@code unit}
* @param unit a {@code TimeUnit} determining how to interpret the timeout parameter
* @return the number of elements transferred
*/
@Beta
@CanIgnoreReturnValue
@GwtIncompatible // BlockingQueue
public static <E> int drainUninterruptibly(
BlockingQueue<E> q,
Collection<? super E> buffer,
int numElements,
long timeout,
TimeUnit unit) {
Preconditions.checkNotNull(buffer);
long deadline = System.nanoTime() + unit.toNanos(timeout);
int added = 0;
boolean interrupted = false;
try {
while (added < numElements) {
// we could rely solely on #poll, but #drainTo might be more efficient when there are
// multiple elements already available (e.g. LinkedBlockingQueue#drainTo locks only once)
added += q.drainTo(buffer, numElements - added);
if (added < numElements) { // not enough elements immediately available; will have to poll
E e; // written exactly once, by a successful (uninterrupted) invocation of #poll
while (true) {
try {
e = q.poll(deadline - System.nanoTime(), TimeUnit.NANOSECONDS);
break;
} catch (InterruptedException ex) {
interrupted = true; // note interruption and retry
}
}
if (e == null) {
break; // we already waited enough, and there are no more elements in sight
}
buffer.add(e);
added++;
}
}
} finally {
if (interrupted) {
Thread.currentThread().interrupt();
}
}
return added;
}