Java Code Examples for java.util.concurrent.LinkedBlockingDeque#poll()

The following examples show how to use java.util.concurrent.LinkedBlockingDeque#poll() . These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source Project: SpringMVC-Project   File: ChatController.java    License: MIT License 6 votes vote down vote up
@Override
public void run() {
    LinkedBlockingDeque<MessageVo> messageQueue = MESSAGE_QUEUE_MAP.get(userId);
    BaseResponse<List<MessageVo>> response = new BaseResponse<>();
    List<MessageVo> list = Lists.newArrayList();

    MessageVo vo;
    try {
        if ((vo = messageQueue.poll(timeout, TimeUnit.MILLISECONDS)) != null) {
            list.add(vo);
            //一次最多取10条信息
            for (int i = 0; i < 9; i++) {
                vo = messageQueue.poll();
                if (vo == null) {
                    break;
                }
                list.add(vo);
            }
        }
    } catch (InterruptedException e) {
        e.printStackTrace();
    }

    response.setData(list);
    deferredResult.setResult(response);
}
 
Example 2
/**
 * drainTo(c, n) empties first min(n, size) elements of queue into c
 */
public void testDrainToN() {
    LinkedBlockingDeque q = new LinkedBlockingDeque();
    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);
    }
}
 
Example 3
Source Project: j2objc   File: LinkedBlockingDequeTest.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * drainTo(c, n) empties first min(n, size) elements of queue into c
 */
public void testDrainToN() {
    LinkedBlockingDeque q = new LinkedBlockingDeque();
    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);
    }
}
 
Example 4
Source Project: airpal   File: LocalJobHistoryStore.java    License: Apache License 2.0 6 votes vote down vote up
@Override
protected boolean evictItem(LinkedBlockingDeque<Job> deque) {
    final Job job = deque.poll();

    if (job != null) {
        if (job.getState().isDone()) {
            return true;
        } else {
            boolean addResult = add(job);
            boolean secondEviction = evictItem(deque);

            return addResult && secondEviction;
        }
    } else {
        return false;
    }
}
 
Example 5
/**
 * element returns next element, or throws NSEE if empty
 */
public void testElement() {
    LinkedBlockingDeque q = populatedDeque(SIZE);
    for (int i = 0; i < SIZE; ++i) {
        assertEquals(i, q.element());
        q.poll();
    }
    try {
        q.element();
        shouldThrow();
    } catch (NoSuchElementException success) {}
}
 
Example 6
/**
 * contains(x) reports true when elements added but not yet removed
 */
public void testContains() {
    LinkedBlockingDeque q = populatedDeque(SIZE);
    for (int i = 0; i < SIZE; ++i) {
        assertTrue(q.contains(new Integer(i)));
        q.poll();
        assertFalse(q.contains(new Integer(i)));
    }
}
 
Example 7
Source Project: j2objc   File: LinkedBlockingDequeTest.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * element returns next element, or throws NSEE if empty
 */
public void testElement() {
    LinkedBlockingDeque q = populatedDeque(SIZE);
    for (int i = 0; i < SIZE; ++i) {
        assertEquals(i, q.element());
        q.poll();
    }
    try {
        q.element();
        shouldThrow();
    } catch (NoSuchElementException success) {}
}
 
Example 8
Source Project: j2objc   File: LinkedBlockingDequeTest.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * contains(x) reports true when elements added but not yet removed
 */
public void testContains() {
    LinkedBlockingDeque q = populatedDeque(SIZE);
    for (int i = 0; i < SIZE; ++i) {
        assertTrue(q.contains(new Integer(i)));
        q.poll();
        assertFalse(q.contains(new Integer(i)));
    }
}
 
Example 9
Source Project: pyramid   File: RegressionTree.java    License: Apache License 2.0 5 votes vote down vote up
public List<Integer> getFeatureIndices(){
    List<Integer> featureIndices = new ArrayList<Integer>();
    LinkedBlockingDeque<Node> queue = new LinkedBlockingDeque<Node>();
    queue.offer(this.root);
    while(queue.size()!=0){
        Node node = queue.poll();
        if (! node.isLeaf()){
            //don't add the feature for leaf node, as it is useless
            featureIndices.add(node.getFeatureIndex());
            queue.offer(node.getLeftChild());
            queue.offer(node.getRightChild());
        }
    }
    return featureIndices;
}
 
Example 10
Source Project: jstorm   File: TimeCacheQueue.java    License: Apache License 2.0 5 votes vote down vote up
public K poll() {
    synchronized (_lock) {
        Iterator<LinkedBlockingDeque<K>> itr = _buckets.descendingIterator();
        while (itr.hasNext()) {
            LinkedBlockingDeque<K> bucket = itr.next();
            K entry = bucket.poll();
            if (entry != null) {
                return entry;
            }
        }

        return null;
    }
}
 
Example 11
Source Project: twister2   File: LMDBDataWriter.java    License: Apache License 2.0 4 votes vote down vote up
@Override
  public void run() {

    Txn<ByteBuffer> writeTxn = null;
    Map<Integer, Cursor<ByteBuffer>> openCoursors = new HashMap<>();
    List<Integer> opIDs = new ArrayList<>();
    int opID = 0;
    Cursor<ByteBuffer> c = null;
    int count = 0;
    while (currentDbiCount == 0) {
      if (dbMapRef.size() > 0 && dataQueueMapRef.size() > 0) {
        writeTxn = this.envRef.txnWrite();
        for (Map.Entry<Integer, Dbi<ByteBuffer>> dbiEntry : dbMapRef.entrySet()) {
          opID = dbiEntry.getKey();
          c = dbiEntry.getValue().openCursor(writeTxn);
          currentDbiCount++;
        }
      }
    }

    Pair<byte[], byte[]> tempPair = null;
    LinkedBlockingDeque<Pair<byte[], byte[]>> dataQueue = dataQueueMapRef.get(opID);

    while (true) {

//      if (dbMapRef.size() > currentDbiCount) {
//        for (Map.Entry<Integer, Dbi<ByteBuffer>> dbiEntry : dbMapRef.entrySet()) {
//          if (!openCoursors.containsKey(dbiEntry.getKey())) {
//            openCoursors.put(dbiEntry.getKey(), dbiEntry.getValue().openCursor(writeTxn));
//            opIDs.add(dbiEntry.getKey());
//            currentDbiCount++;
//          }
//        }
//      }
//
//      if(currentDbiCount == 0){
//        Thread.yield();
//      }
      tempPair = dataQueue.poll();
      if (tempPair != null) {
        setupThreadLocalBuffers(tempPair.getKey().length, tempPair.getValue().length);
        keyBuffer.put(tempPair.getKey());
        keyBuffer.flip();

        dataBuffer.putInt(tempPair.getValue().length);
        dataBuffer.put(tempPair.getValue());
        dataBuffer.flip();

        c.put(keyBuffer, dataBuffer);
        LMDBMemoryManager.needsCommitWriter = true;
        count++;
        if (count % 2000000 == 0 && count > 0) {
          writeTxn.commit();
          writeTxn.close();
          writeTxn = this.envRef.txnWrite();
          c = dbMapRef.get(opID).openCursor(writeTxn);
          LMDBMemoryManager.needsCommitWriter = false;
          LMDBMemoryManager.needsCommitReader = false;
          System.out.println("added 2  mil to mm");
        }
      }

      if (LMDBMemoryManager.needsCommitReader) {
        writeTxn.commit();
        writeTxn.close();
        writeTxn = this.envRef.txnWrite();
        c = dbMapRef.get(opID).openCursor(writeTxn);
        LMDBMemoryManager.needsCommitWriter = false;
        LMDBMemoryManager.needsCommitReader = false;
      }

    }


  }