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


Java ConcurrentLinkedDeque.offer方法代码示例

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


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

示例1: testOfferNull

import java.util.concurrent.ConcurrentLinkedDeque; //导入方法依赖的package包/类
/**
 * offer(null) throws NPE
 */
public void testOfferNull() {
    ConcurrentLinkedDeque q = new ConcurrentLinkedDeque();
    try {
        q.offer(null);
        shouldThrow();
    } catch (NullPointerException success) {}
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:11,代码来源:ConcurrentLinkedDequeTest.java

示例2: testBufferSizeGrowWithMultiThread

import java.util.concurrent.ConcurrentLinkedDeque; //导入方法依赖的package包/类
@Test
public void testBufferSizeGrowWithMultiThread() throws Exception {
  final ConcurrentLinkedDeque<ByteBuffer> bufferQueue = new ConcurrentLinkedDeque<ByteBuffer>();
  int takeBufferThreadsCount = 30;
  int putBufferThreadsCount = 1;
  Thread takeBufferThreads[] = new Thread[takeBufferThreadsCount];
  for (int i = 0; i < takeBufferThreadsCount; i++) {
    takeBufferThreads[i] = new Thread(new Runnable() {
      @Override
      public void run() {
        while (true) {
          ByteBuffer buffer = reservoir.getBuffer();
          try {
            Thread.sleep(5);
          } catch (InterruptedException e) {
            break;
          }
          bufferQueue.offer(buffer);
          if (Thread.currentThread().isInterrupted()) break;
        }
      }
    });
  }

  Thread putBufferThread[] = new Thread[putBufferThreadsCount];
  for (int i = 0; i < putBufferThreadsCount; i++) {
    putBufferThread[i] = new Thread(new Runnable() {
      @Override
      public void run() {
        while (true) {
          ByteBuffer buffer = bufferQueue.poll();
          if (buffer != null) {
            reservoir.putBuffer(buffer);
          }
          if (Thread.currentThread().isInterrupted()) break;
        }
      }
    });
  }

  for (int i = 0; i < takeBufferThreadsCount; i++) {
    takeBufferThreads[i].start();
  }
  for (int i = 0; i < putBufferThreadsCount; i++) {
    putBufferThread[i].start();
  }
  Thread.sleep(2 * 1000);// Let the threads run for 2 secs
  for (int i = 0; i < takeBufferThreadsCount; i++) {
    takeBufferThreads[i].interrupt();
    takeBufferThreads[i].join();
  }
  for (int i = 0; i < putBufferThreadsCount; i++) {
    putBufferThread[i].interrupt();
    putBufferThread[i].join();
  }
  // None of the BBs we got from pool is growing while in use. So we should not change the
  // runningAverage in pool
  assertEquals(initialByteBufferSize, this.reservoir.runningAverage);
}
 
开发者ID:fengchen8086,项目名称:ditb,代码行数:60,代码来源:TestBoundedByteBufferPool.java


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