当前位置: 首页>>代码示例>>Java>>正文


Java BlockingQueue.poll方法代码示例

本文整理汇总了Java中java.util.concurrent.BlockingQueue.poll方法的典型用法代码示例。如果您正苦于以下问题:Java BlockingQueue.poll方法的具体用法?Java BlockingQueue.poll怎么用?Java BlockingQueue.poll使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在java.util.concurrent.BlockingQueue的用法示例。


在下文中一共展示了BlockingQueue.poll方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: rejectedExecution

import java.util.concurrent.BlockingQueue; //导入方法依赖的package包/类
@Override
public void rejectedExecution(Runnable runnable, ThreadPoolExecutor executor) {
    if (threadName != null) {
        LOG.error("tccTransaction Thread pool [{}] is exhausted, executor={}", threadName, executor.toString());
    }

    if (runnable instanceof RejectedRunnable) {
        ((RejectedRunnable) runnable).rejected();
    } else {
        if (!executor.isShutdown()) {
            BlockingQueue<Runnable> queue = executor.getQueue();
            int discardSize = queue.size() >> 1;
            for (int i = 0; i < discardSize; i++) {
                queue.poll();
            }

            try {
                queue.put(runnable);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}
 
开发者ID:yu199195,项目名称:happylifeplat-tcc,代码行数:25,代码来源:RejectedPolicy.java

示例2: take

import java.util.concurrent.BlockingQueue; //导入方法依赖的package包/类
@Override
public E take() throws InterruptedException {
  int startIdx = this.multiplexer.getAndAdvanceCurrentIndex();

  takeLock.lockInterruptibly();
  try {
    // Wait while queue is empty
    for (;;) {
      BlockingQueue<E> q = this.getFirstNonEmptyQueue(startIdx);
      if (q != null) {
        // Got queue, so return if we can poll out an object
        E e = q.poll();
        if (e != null) {
          return e;
        }
      }

      notEmpty.await();
    }
  } finally {
    takeLock.unlock();
  }
}
 
开发者ID:nucypher,项目名称:hadoop-oss,代码行数:24,代码来源:FairCallQueue.java

示例3: rejectedExecution

import java.util.concurrent.BlockingQueue; //导入方法依赖的package包/类
@Override
public void rejectedExecution(Runnable runnable, ThreadPoolExecutor executor) {
    if (threadName != null) {
        LOG.error("txTransaction Thread pool [{}] is exhausted, executor={}", threadName, executor.toString());
    }

    if (runnable instanceof RejectedRunnable) {
        ((RejectedRunnable) runnable).rejected();
    } else {
        if (!executor.isShutdown()) {
            BlockingQueue<Runnable> queue = executor.getQueue();
            int discardSize = queue.size() >> 1;
            for (int i = 0; i < discardSize; i++) {
                queue.poll();
            }

            try {
                queue.put(runnable);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}
 
开发者ID:yu199195,项目名称:happylifeplat-transaction,代码行数:25,代码来源:RejectedPolicy.java

示例4: postSynchronousMessage

import java.util.concurrent.BlockingQueue; //导入方法依赖的package包/类
@Override
public <T> T postSynchronousMessage(String taskId, Serializable message, long millis)
{
	String messageId = UUID.randomUUID().toString();
	SimpleMessage msg = new SimpleMessage(messageId, message);
	BlockingQueue<SimpleMessage> blockingQueue = new LinkedBlockingQueue<SimpleMessage>(1);
	messageResponses.put(messageId, blockingQueue);
	processMessage(taskId, msg, true);
	try
	{
		SimpleMessage poll = blockingQueue.poll(millis, TimeUnit.MILLISECONDS);
		if( poll != null )
		{
			return poll.getContents();
		}
		throw new TimeoutException("Timed out waiting for message response");
	}
	catch( InterruptedException e )
	{
		throw new RuntimeException(e);
	}
}
 
开发者ID:equella,项目名称:Equella,代码行数:23,代码来源:LocalTaskServiceImpl.java

示例5: waitForResponse

import java.util.concurrent.BlockingQueue; //导入方法依赖的package包/类
private <T> T waitForResponse(long millis, BlockingQueue<SimpleMessage> blockingQueue)
{
	try
	{
		SimpleMessage poll = blockingQueue.poll(millis, TimeUnit.MILLISECONDS);
		if( poll != null )
		{
			return poll.getContents();
		}
		throw new TimeoutException("Timed out waiting for message response");
	}
	catch( InterruptedException e )
	{
		throw new RuntimeException(e);
	}
}
 
开发者ID:equella,项目名称:Equella,代码行数:17,代码来源:ClusteredTaskServiceImpl.java

示例6: execute

import java.util.concurrent.BlockingQueue; //导入方法依赖的package包/类
@Override
public JobResult execute(BlockingQueue<String> jobPathArray) {
	String currentThreadName = Thread.currentThread().getName();
	logger.info(" [cz88.net] 线程[" + currentThreadName + "] 爬虫JOB 开始工作 ");
	JobResult jobResult = new JobResult();
	String url = null  ;
	try {
		while ((url = jobPathArray.poll()) != null) {
			BaseUtil.getInstance().getRedisClient().lpush(Constant.WEBSITE_CODE_QUEUE_CZ88, url) ;
		}
	} catch (Exception e) {
		logger.info("Redis 连接异常 , 网络是否畅通 , 服务是否启动") ;
	}
	if (null == url) {
		logger.info(" [cz88.net] 线程[" + currentThreadName + "] 发现队列为空 停止工作 ");
		CrawlersTaskPool.sharedInstance().getExecutor().shutdown();
	}
	logger.info(" [cz88.net] 线程[" + currentThreadName + "] 爬虫JOB 结束工作 ");
	return jobResult ;
}
 
开发者ID:m18507308080,项目名称:bohemia,代码行数:21,代码来源:Cz88Job.java

示例7: testDrainTimesOut

import java.util.concurrent.BlockingQueue; //导入方法依赖的package包/类
private void testDrainTimesOut(BlockingQueue<Object> q) throws Exception {
  for (boolean interruptibly : new boolean[] {true, false}) {
    assertEquals(0, Queues.drain(q, ImmutableList.of(), 1, 10, MILLISECONDS));

    Producer producer = new Producer(q, 1);
    // producing one, will ask for two
    Future<?> producerThread = threadPool.submit(producer);
    producer.beganProducing.await();

    // make sure we time out
    Stopwatch timer = Stopwatch.createStarted();

    int drained = drain(q, newArrayList(), 2, 10, MILLISECONDS, interruptibly);
    assertThat(drained).isAtMost(1);

    assertThat(timer.elapsed(MILLISECONDS)).isAtLeast(10L);

    // If even the first one wasn't there, clean up so that the next test doesn't see an element.
    producerThread.cancel(true);
    producer.doneProducing.await();
    if (drained == 0) {
      q.poll(); // not necessarily there if producer was interrupted
    }
  }
}
 
开发者ID:zugzug90,项目名称:guava-mock,代码行数:26,代码来源:QueuesTest.java

示例8: rejectedExecution

import java.util.concurrent.BlockingQueue; //导入方法依赖的package包/类
@Override
public void rejectedExecution(Runnable runnable, ThreadPoolExecutor executor) {
    if (threadName != null) {
        LOG.error("txTransaction Thread pool [{}] is exhausted, executor={}", threadName, executor.toString());
    }

    if (!executor.isShutdown()) {
        BlockingQueue<Runnable> queue = executor.getQueue();
        int discardSize = queue.size() >> 1;
        for (int i = 0; i < discardSize; i++) {
            queue.poll();
        }
        queue.offer(runnable);
    }
}
 
开发者ID:yu199195,项目名称:happylifeplat-transaction,代码行数:16,代码来源:DiscardedPolicy.java

示例9: testSamplingError

import java.util.concurrent.BlockingQueue; //导入方法依赖的package包/类
@Test
public void testSamplingError() {
  KafkaCruiseControlConfig config = new KafkaCruiseControlConfig(getLoadMonitorProperties());
  Metadata metadata = new Metadata();
  MetadataClient metadataClient = new MetadataClient(config, metadata, -1L, TIME);
  MockMetricSampleAggregator mockMetricSampleAggregator = new MockMetricSampleAggregator(config, metadata);
  List<MetricSampler> samplers = new ArrayList<>();
  MetricRegistry dropwizardMetricRegistry = new MetricRegistry();
  for (int i = 0; i < NUM_METRIC_FETCHERS; i++) {
    samplers.add(new MockSampler(i));
  }
  MetricFetcherManager fetcherManager = new MetricFetcherManager(config, mockMetricSampleAggregator, metadataClient,
                                                                 TIME, dropwizardMetricRegistry, samplers);
  LoadMonitorTaskRunner loadMonitorTaskRunner =
      new LoadMonitorTaskRunner(config, fetcherManager, mockMetricSampleAggregator, metadataClient, TIME);
  while (metadata.fetch().topics().size() < 100) {
    metadataClient.refreshMetadata();
  }
  loadMonitorTaskRunner.start(true);

  int numSamples = 0;
  long startMs = System.currentTimeMillis();
  BlockingQueue<PartitionMetricSample> sampleQueue = mockMetricSampleAggregator.metricSampleQueue();
  while (numSamples < (NUM_PARTITIONS * NUM_TOPICS) * 10 && System.currentTimeMillis() < startMs + 10000) {
    PartitionMetricSample sample = sampleQueue.poll();
    if (sample != null) {
      numSamples++;
    }
  }
  // We should have NUM_METRIC_FETCHER rounds of sampling. The first round only has one metric fetcher returns
  // samples, two fetchers return samples for the second round, three for the third and four for the forth round.
  // So the first round only has 1/4 of the total samples, then 2/4, 3/4 and all the samples.
  int expectedNumSamples = 0;
  for (int i = 0; i < NUM_METRIC_FETCHERS; i++) {
    expectedNumSamples += (NUM_TOPICS * NUM_PARTITIONS) * (i + 1) / NUM_METRIC_FETCHERS;
  }
  assertEquals("Only see " + numSamples + " samples. Expecting " + expectedNumSamples + " samples",
      expectedNumSamples, numSamples);
  fetcherManager.shutdown();
}
 
开发者ID:linkedin,项目名称:cruise-control,代码行数:41,代码来源:LoadMonitorTaskRunnerTest.java

示例10: 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;
}
 
开发者ID:s-store,项目名称:sstore-soft,代码行数:40,代码来源:Queues.java

示例11: 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;
}
 
开发者ID:s-store,项目名称:sstore-soft,代码行数:50,代码来源:Queues.java

示例12: poll

import java.util.concurrent.BlockingQueue; //导入方法依赖的package包/类
@Override
public E poll(long timeout, TimeUnit unit)
    throws InterruptedException {

  int startIdx = this.multiplexer.getAndAdvanceCurrentIndex();

  long nanos = unit.toNanos(timeout);
  takeLock.lockInterruptibly();
  try {
    for (;;) {
      BlockingQueue<E> q = this.getFirstNonEmptyQueue(startIdx);
      if (q != null) {
        E e = q.poll();
        if (e != null) {
          // Escape condition: there might be something available
          return e;
        }
      }

      if (nanos <= 0) {
        // Wait has elapsed
        return null;
      }

      try {
        // Now wait on the condition for a bit. If we get
        // spuriously awoken we'll re-loop
        nanos = notEmpty.awaitNanos(nanos);
      } catch (InterruptedException ie) {
        notEmpty.signal(); // propagate to a non-interrupted thread
        throw ie;
      }
    }
  } finally {
    takeLock.unlock();
  }
}
 
开发者ID:nucypher,项目名称:hadoop-oss,代码行数:38,代码来源:FairCallQueue.java

示例13: getResult

import java.util.concurrent.BlockingQueue; //导入方法依赖的package包/类
/**
 * 获取结果
 * @param blockingQueue
 * @param timeout
 * @param unit
 * @return
 * @throws InterruptedIOException
 */
public static Object getResult(BlockingQueue<Object> blockingQueue,long timeout, TimeUnit unit) throws InterruptedIOException {
    Object result;
    try {
        result = blockingQueue.poll(timeout, unit);
        if (result == null) {
            if (!blockingQueue.offer("")) {
                result = blockingQueue.take();
            }
        }
    } catch (InterruptedException e) {
        throw ExceptionUtil.initCause(new InterruptedIOException(e.getMessage()), e);
    }
    return result;
}
 
开发者ID:DevopsJK,项目名称:SuitAgent,代码行数:23,代码来源:BlockingQueueUtil.java

示例14: poll

import java.util.concurrent.BlockingQueue; //导入方法依赖的package包/类
/**
 * @return - returns the next Message to be processed out of the queue.
 */
@Override
public Message poll() {
    // If its null, or we hit the end, reset it.
    if (consumerIdIterator == null || !consumerIdIterator.hasNext()) {
        consumerIdIterator = messageBuffer.keySet().iterator();
    }

    // Try every buffer until we hit the end.
    Message returnMsg = null;
    while (returnMsg == null && consumerIdIterator.hasNext()) {

        // Advance iterator
        final VirtualSpoutIdentifier nextConsumerId = consumerIdIterator.next();

        // Find our buffer
        final BlockingQueue<Message> queue = messageBuffer.get(nextConsumerId);

        // We missed?
        if (queue == null) {
            logger.info("Non-existent queue found, resetting iterator.");
            consumerIdIterator = messageBuffer.keySet().iterator();
            continue;
        }
        returnMsg = queue.poll();
    }
    return returnMsg;
}
 
开发者ID:salesforce,项目名称:storm-dynamic-spout,代码行数:31,代码来源:RoundRobinBuffer.java

示例15: getRandomFrameFromVideo

import java.util.concurrent.BlockingQueue; //导入方法依赖的package包/类
/**
 * Gets a random frame from the video file
 *
 * @param f the video file
 * @return an image frame from the video file
 * @throws Exception if there are any processing errors
 */
private Image getRandomFrameFromVideo(File f) throws Exception {
    BlockingQueue<Image> queue = new DisruptorBlockingQueue<>(MAX_QUEUE_SIZE);
    final VideoLoadedLock videoLoadedLock = new VideoLoadedLock();
    OptionsObject.getInstance().getVideoImportEngine().convertVideoToImages(f, true, queue, videoLoadedLock);
    videoLoadedLock.waitFor();
    return queue.poll();
}
 
开发者ID:KodeMunkie,项目名称:imagetozxspec,代码行数:15,代码来源:WorkManager.java


注:本文中的java.util.concurrent.BlockingQueue.poll方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。