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

x33g5p2x  于2022-01-23 转载在 其他  
字(6.8k)|赞(0)|评价(0)|浏览(162)

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

LinkedBlockingQueue.remainingCapacity介绍

[英]Returns the number of additional elements that this queue can ideally (in the absence of memory or resource constraints) accept without blocking. This is always equal to the initial capacity of this queue less the current size of this queue.

Note that you cannot always tell if an attempt to insert an element will succeed by inspecting remainingCapacitybecause it may be the case that another thread is about to insert or remove an element.
[中]返回此队列理想情况下(在没有内存或资源约束的情况下)可以接受而不阻塞的其他元素数。这始终等于此队列的初始容量减去此队列的当前大小。
请注意,您不能总是通过检查RemainingCapacity来判断插入元素的尝试是否成功,因为可能是另一个线程即将插入或删除元素。

代码示例

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

/**
 * A new queue has the indicated capacity, or Integer.MAX_VALUE if
 * none given
 */
public void testConstructor1() {
  assertEquals(SIZE, new LinkedBlockingQueue(SIZE).remainingCapacity());
  assertEquals(Integer.MAX_VALUE, new LinkedBlockingQueue().remainingCapacity());
}

代码示例来源:origin: Netflix/conductor

int realPollCount = Math.min(workerQueue.remainingCapacity(), pollCount);
if (realPollCount <= 0) {
  logger.warn("All workers are busy, not polling. queue size: {}, max: {}, task:{}", workerQueue.size(), workerQueueSize, taskName);

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

public void realRun() throws InterruptedException {
  assertFalse(q.offer(three));
  threadsStarted.await();
  assertTrue(q.offer(three, LONG_DELAY_MS, MILLISECONDS));
  assertEquals(0, q.remainingCapacity());
}});

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

public void testNewLinkedBlockingQueueCapacity() {
 try {
  Queues.newLinkedBlockingQueue(0);
  fail("Should have thrown IllegalArgumentException");
 } catch (IllegalArgumentException expected) {
  // any capacity less than 1 should throw IllegalArgumentException
 }
 assertEquals(1, Queues.newLinkedBlockingQueue(1).remainingCapacity());
 assertEquals(11, Queues.newLinkedBlockingQueue(11).remainingCapacity());
}

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

/**
 * add succeeds if not full; throws IllegalStateException if full
 */
public void testAdd() {
  LinkedBlockingQueue q = new LinkedBlockingQueue(SIZE);
  for (int i = 0; i < SIZE; ++i)
    assertTrue(q.add(new Integer(i)));
  assertEquals(0, q.remainingCapacity());
  try {
    q.add(new Integer(SIZE));
    shouldThrow();
  } catch (IllegalStateException success) {}
}

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

/**
 * all elements successfully put are contained
 */
public void testPut() throws InterruptedException {
  LinkedBlockingQueue q = new LinkedBlockingQueue(SIZE);
  for (int i = 0; i < SIZE; ++i) {
    Integer x = new Integer(i);
    q.put(x);
    assertTrue(q.contains(x));
  }
  assertEquals(0, q.remainingCapacity());
}

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

public void realRun() throws InterruptedException {
  for (int i = 0; i < SIZE; ++i)
    q.put(i);
  assertEquals(SIZE, q.size());
  assertEquals(0, q.remainingCapacity());
  Thread.currentThread().interrupt();
  try {
    q.put(99);
    shouldThrow();
  } catch (InterruptedException success) {}
  assertFalse(Thread.interrupted());
  pleaseInterrupt.countDown();
  try {
    q.put(99);
    shouldThrow();
  } catch (InterruptedException success) {}
  assertFalse(Thread.interrupted());
}});

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

/**
 * iterator ordering is FIFO
 */
public void testIteratorOrdering() {
  final LinkedBlockingQueue q = new LinkedBlockingQueue(3);
  q.add(one);
  q.add(two);
  q.add(three);
  assertEquals(0, q.remainingCapacity());
  int k = 0;
  for (Iterator it = q.iterator(); it.hasNext();) {
    assertEquals(++k, it.next());
  }
  assertEquals(3, k);
}

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

getCompressedQueries().add(new CompressedItem("\n" + logItem + "\n", decompressStatementForLog));
  } else {
    if (getCompressedQueries().remainingCapacity() == 0) {
      getCompressedQueries().poll();
  getQueries().add("\n" + logItem + "\n");
} else {
  if (getQueries().remainingCapacity() == 0) {
    getQueries().poll();

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

/**
 * Queue transitions from empty to full when elements added
 */
public void testEmptyFull() {
  LinkedBlockingQueue q = new LinkedBlockingQueue(2);
  assertTrue(q.isEmpty());
  assertEquals("should have room for 2", 2, q.remainingCapacity());
  q.add(one);
  assertFalse(q.isEmpty());
  q.add(two);
  assertFalse(q.isEmpty());
  assertEquals(0, q.remainingCapacity());
  assertFalse(q.offer(three));
}

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

assertEquals(0, q.remainingCapacity());
assertEquals(0, q.take());
t.interrupt();
awaitTermination(t);
assertEquals(0, q.remainingCapacity());

代码示例来源:origin: Netflix/conductor

try{
  int realPollCount = Math.min(workerQueue.remainingCapacity(), worker.getPollCount());
  if (realPollCount <= 0) {
    logger.warn("All workers are busy, not polling. queue size = {}, max = {}", workerQueue.size(), workerQueueSize);

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

/**
 * Returns a new queue of given size containing consecutive
 * Integers 0 ... n - 1.
 */
private static LinkedBlockingQueue<Integer> populatedQueue(int n) {
  LinkedBlockingQueue<Integer> q = new LinkedBlockingQueue<>(n);
  assertTrue(q.isEmpty());
  for (int i = 0; i < n; i++)
    assertTrue(q.offer(new Integer(i)));
  assertFalse(q.isEmpty());
  assertEquals(0, q.remainingCapacity());
  assertEquals(n, q.size());
  assertEquals((Integer) 0, q.peek());
  return q;
}

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

awaitTermination(t);
assertEquals(SIZE, q.size());
assertEquals(0, q.remainingCapacity());

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

/**
 * clear removes all elements
 */
public void testClear() {
  LinkedBlockingQueue q = populatedQueue(SIZE);
  q.clear();
  assertTrue(q.isEmpty());
  assertEquals(0, q.size());
  assertEquals(SIZE, q.remainingCapacity());
  q.add(one);
  assertFalse(q.isEmpty());
  assertTrue(q.contains(one));
  q.clear();
  assertTrue(q.isEmpty());
}

代码示例来源:origin: apache/tinkerpop

public MapReducePool(final MapReduce mapReduce, final int poolSize) {
  this.pool = new LinkedBlockingQueue<>(poolSize);
  while (this.pool.remainingCapacity() > 0) {
    this.pool.add(mapReduce.clone());
  }
}

代码示例来源:origin: apache/tinkerpop

public VertexProgramPool(final VertexProgram vertexProgram, final int poolSize) {
  this.pool = new LinkedBlockingQueue<>(poolSize);
  while (this.pool.remainingCapacity() > 0) {
    this.pool.add(vertexProgram.clone());
  }
}

代码示例来源:origin: com.tencent.tars/tars-core

@Override
public int remainingCapacity() {
  if (forcedRemainingCapacity != null) {
    // ThreadPoolExecutor.setCorePoolSize checks that
    // remainingCapacity==0 to allow to interrupt idle threads
    // I don't see why, but this hack allows to conform to this
    // "requirement"
    return forcedRemainingCapacity.intValue();
  }
  return super.remainingCapacity();
}

代码示例来源:origin: org.apache.tinkerpop/gremlin-core

public VertexProgramPool(final VertexProgram vertexProgram, final int poolSize) {
  this.pool = new LinkedBlockingQueue<>(poolSize);
  while (this.pool.remainingCapacity() > 0) {
    this.pool.add(vertexProgram.clone());
  }
}

代码示例来源:origin: org.apache.tinkerpop/gremlin-core

public MapReducePool(final MapReduce mapReduce, final int poolSize) {
  this.pool = new LinkedBlockingQueue<>(poolSize);
  while (this.pool.remainingCapacity() > 0) {
    this.pool.add(mapReduce.clone());
  }
}

相关文章

微信公众号

最新文章

更多