當前位置: 首頁>>代碼示例>>Java>>正文


Java LinkedBlockingQueue.poll方法代碼示例

本文整理匯總了Java中java.util.concurrent.LinkedBlockingQueue.poll方法的典型用法代碼示例。如果您正苦於以下問題:Java LinkedBlockingQueue.poll方法的具體用法?Java LinkedBlockingQueue.poll怎麽用?Java LinkedBlockingQueue.poll使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在java.util.concurrent.LinkedBlockingQueue的用法示例。


在下文中一共展示了LinkedBlockingQueue.poll方法的4個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。

示例1: allocateCommBuffer

import java.util.concurrent.LinkedBlockingQueue; //導入方法依賴的package包/類
public static ByteBuffer allocateCommBuffer(int size, Socket sock) {
  // I expect that size will almost always be the same value
  if (sock.getChannel() == null) {
    // The socket this commBuffer will be used for is old IO (it has no channel).
    // So the commBuffer should be heap based.
    return ByteBuffer.allocate(size);
  }
  LinkedBlockingQueue<ByteBuffer> q = commBufferMap.get(size);
  ByteBuffer result = null;
  if (q != null) {
    result = q.poll();
  }
  if (result == null) {
    result = ByteBuffer.allocateDirect(size);
  } else {
    result.position(0);
    result.limit(result.capacity());
  }
  return result;
}
 
開發者ID:ampool,項目名稱:monarch,代碼行數:21,代碼來源:ServerConnection.java

示例2: testDrainToN

import java.util.concurrent.LinkedBlockingQueue; //導入方法依賴的package包/類
/**
 * drainTo(c, n) empties first min(n, size) elements of queue into c
 */
public void testDrainToN() {
    LinkedBlockingQueue q = new LinkedBlockingQueue();
    for (int i = 0; i < SIZE + 2; ++i) {
        for (int j = 0; j < SIZE; j++)
            assertTrue(q.offer(new Integer(j)));
        ArrayList l = new ArrayList();
        q.drainTo(l, i);
        int k = (i < SIZE) ? i : SIZE;
        assertEquals(k, l.size());
        assertEquals(SIZE - k, q.size());
        for (int j = 0; j < k; ++j)
            assertEquals(l.get(j), new Integer(j));
        do {} while (q.poll() != null);
    }
}
 
開發者ID:AdoptOpenJDK,項目名稱:openjdk-jdk10,代碼行數:19,代碼來源:LinkedBlockingQueueTest.java

示例3: testContains

import java.util.concurrent.LinkedBlockingQueue; //導入方法依賴的package包/類
/**
 * contains(x) reports true when elements added but not yet removed
 */
public void testContains() {
    LinkedBlockingQueue q = populatedQueue(SIZE);
    for (int i = 0; i < SIZE; ++i) {
        assertTrue(q.contains(new Integer(i)));
        q.poll();
        assertFalse(q.contains(new Integer(i)));
    }
}
 
開發者ID:AdoptOpenJDK,項目名稱:openjdk-jdk10,代碼行數:12,代碼來源:LinkedBlockingQueueTest.java

示例4: getAtMost

import java.util.concurrent.LinkedBlockingQueue; //導入方法依賴的package包/類
/**
 * This removes the "num" values currently at the head of the Queue for the
 * provided key. Will immediately fire the Queue filler function if key
 * does not exist
 * How many values are actually returned is governed by the
 * <code>SyncGenerationPolicy</code> specified by the user.
 * @param keyName String key name
 * @param num Minimum number of values to return.
 * @return List<E> values returned
 * @throws IOException
 * @throws ExecutionException
 */
public List<E> getAtMost(String keyName, int num) throws IOException,
    ExecutionException {
  LinkedBlockingQueue<E> keyQueue = keyQueues.get(keyName);
  // Using poll to avoid race condition..
  LinkedList<E> ekvs = new LinkedList<E>();
  try {
    for (int i = 0; i < num; i++) {
      E val = keyQueue.poll();
      // If queue is empty now, Based on the provided SyncGenerationPolicy,
      // figure out how many new values need to be generated synchronously
      if (val == null) {
        // Synchronous call to get remaining values
        int numToFill = 0;
        switch (policy) {
        case ATLEAST_ONE:
          numToFill = (ekvs.size() < 1) ? 1 : 0;
          break;
        case LOW_WATERMARK:
          numToFill =
              Math.min(num, (int) (lowWatermark * numValues)) - ekvs.size();
          break;
        case ALL:
          numToFill = num - ekvs.size();
          break;
        }
        // Synchronous fill if not enough values found
        if (numToFill > 0) {
          refiller.fillQueueForKey(keyName, ekvs, numToFill);
        }
        // Asynch task to fill > lowWatermark
        if (i <= (int) (lowWatermark * numValues)) {
          submitRefillTask(keyName, keyQueue);
        }
        return ekvs;
      }
      ekvs.add(val);
    }
  } catch (Exception e) {
    throw new IOException("Exeption while contacting value generator ", e);
  }
  return ekvs;
}
 
開發者ID:nucypher,項目名稱:hadoop-oss,代碼行數:55,代碼來源:ValueQueue.java


注:本文中的java.util.concurrent.LinkedBlockingQueue.poll方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。