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

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

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

BlockingQueue.offer介绍

[英]Inserts the specified element into this queue if it is possible to do so immediately without violating capacity restrictions, returning true upon success and false if no space is currently available. When using a capacity-restricted queue, this method is generally preferable to #add, which can fail to insert an element only by throwing an exception.
[中]如果可以在不违反容量限制的情况下立即将指定元素插入此队列,则在成功时返回true,如果当前没有可用空间,则返回false。使用容量受限队列时,此方法通常优于#add,后者仅通过引发异常才能插入元素。

代码示例

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

@Override
public void onSuccess(SubTaskCompleteEvent<ParallelIndexSubTask> completeEvent)
{
 // this callback is called if a task completed wheter it succeeded or not.
 taskCompleteEvents.offer(completeEvent);
}

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

@Override
  public void run() {
    try {
      Result result = invoker.invoke(invocation);
      ref.offer(result);
    } catch (Throwable e) {
      int value = count.incrementAndGet();
      if (value >= selected.size()) {
        ref.offer(e);
      }
    }
  }
});

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

@Override
  public void run() {
    try {
      Result result = invoker.invoke(invocation);
      ref.offer(result);
    } catch (Throwable e) {
      int value = count.incrementAndGet();
      if (value >= selected.size()) {
        ref.offer(e);
      }
    }
  }
});

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

@Override
public boolean offer(E e, long timeout, TimeUnit unit) throws InterruptedException {
 return delegate().offer(e, timeout, unit);
}

代码示例来源:origin: ctripcorp/apollo

public boolean audit(String appId, String clusterName, String dataCenter, String
  ip, String configAppId, String configClusterName, String configNamespace, String releaseKey) {
 return this.audits.offer(new InstanceConfigAuditModel(appId, clusterName, dataCenter, ip,
   configAppId, configClusterName, configNamespace, releaseKey));
}

代码示例来源:origin: ReactiveX/RxJava

@Override
public void onNext(Notification<T> args) {
  if (waiting.getAndSet(0) == 1 || !args.isOnNext()) {
    Notification<T> toOffer = args;
    while (!buf.offer(toOffer)) {
      Notification<T> concurrentItem = buf.poll();
      // in case if we won race condition with onComplete/onError method
      if (concurrentItem != null && !concurrentItem.isOnNext()) {
        toOffer = concurrentItem;
      }
    }
  }
}

代码示例来源:origin: ReactiveX/RxJava

@Override
public void onNext(Notification<T> args) {
  if (waiting.getAndSet(0) == 1 || !args.isOnNext()) {
    Notification<T> toOffer = args;
    while (!buf.offer(toOffer)) {
      Notification<T> concurrentItem = buf.poll();
      // in case if we won race condition with onComplete/onError method
      if (concurrentItem != null && !concurrentItem.isOnNext()) {
        toOffer = concurrentItem;
      }
    }
  }
}

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

public void enqueue(OpentsdbEvent event)
{
 if (!eventQueue.offer(event)) {
  if (countLostEvents.getAndIncrement() % 1000 == 0) {
   log.error(
     "Lost total of [%s] events because of emitter queue is full. Please increase the capacity.",
     countLostEvents.get()
   );
  }
 }
}

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

@Override
public boolean offer(E e, long timeout, TimeUnit unit) throws InterruptedException {
 return delegate().offer(e, timeout, unit);
}

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

@Override
 protected void doAction() {
  assertTrue(queue.offer(EXPECTED_TAKE));
 }
}

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

@Override
public void onNext(Notification<T> args) {
  if (waiting.getAndSet(0) == 1 || !args.isOnNext()) {
    Notification<T> toOffer = args;
    while (!buf.offer(toOffer)) {
      Notification<T> concurrentItem = buf.poll();
      // in case if we won race condition with onComplete/onError method
      if (concurrentItem != null && !concurrentItem.isOnNext()) {
        toOffer = concurrentItem;
      }
    }
  }
}

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

@Override
public void onNext(Notification<T> args) {
  if (waiting.getAndSet(0) == 1 || !args.isOnNext()) {
    Notification<T> toOffer = args;
    while (!buf.offer(toOffer)) {
      Notification<T> concurrentItem = buf.poll();
      // in case if we won race condition with onComplete/onError method
      if (concurrentItem != null && !concurrentItem.isOnNext()) {
        toOffer = concurrentItem;
      }
    }
  }
}

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

@Override
 public void onFailure(Throwable t)
 {
  // this callback is called only when there were some problems in TaskMonitor.
  log.error(t, "Error while running a task for subTaskSpec[%s]", spec);
  taskCompleteEvents.offer(SubTaskCompleteEvent.fail(spec, t));
 }
}

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

private TimedPutQueue(long countdownInMillis) {
 this.queue = new ArrayBlockingQueue<>(1);
 assertTrue(queue.offer("blocksPutCallsUntilRemoved"));
 this.completed = new Completion(countdownInMillis);
 scheduleEnableWrites(this.queue, countdownInMillis);
}

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

@Override
 public Boolean call() throws InterruptedException
 {
  while (!stopTest.get()) {
   q.poll(100, TimeUnit.MILLISECONDS);
   q.offer(new TestObject(2));
  }
  return true;
 }
}

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

public EnableWrites(BlockingQueue<String> queue, long tMinus) {
 super(tMinus);
 assertFalse(queue.isEmpty());
 assertFalse(queue.offer("shouldBeRejected"));
 this.queue = queue;
}

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

@Override
 public Boolean call() throws Exception
 {
  barrier.await();
  Assert.assertTrue(q.offer(obj, delayMS, TimeUnit.MILLISECONDS));
  Assert.assertEquals(q.remainingCapacity(), 0);
  barrier.await();
  q.put(obj);
  return true;
 }
}

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

public void testTakeWithNoWait() {
 Stopwatch stopwatch = Stopwatch.createStarted();
 BlockingQueue<String> queue = new ArrayBlockingQueue<>(1);
 assertTrue(queue.offer(""));
 assertEquals("", takeUninterruptibly(queue));
 assertTimeNotPassed(stopwatch, LONG_DELAY_MS);
}

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

@Test
public void testAddedObjectExceedsCapacity() throws Exception
{
 BlockingQueue<TestObject> q = getQueue(4);
 Assert.assertTrue(q.offer(new TestObject(3)));
 Assert.assertFalse(q.offer(new TestObject(2)));
 Assert.assertFalse(q.offer(new TestObject(2), delayMS, TimeUnit.MILLISECONDS));
}

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

@Test
public void testAddBiggerElementThanCapacityFails()
{
 BlockingQueue<TestObject> q = getQueue(5);
 try {
  q.offer(new TestObject(10));
  Assert.fail();
 }
 catch (IllegalArgumentException success) {
 }
}

相关文章