當前位置: 首頁>>代碼示例>>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;未經允許,請勿轉載。