Java Code Examples for java.util.concurrent.BlockingQueue#toArray()

The following examples show how to use java.util.concurrent.BlockingQueue#toArray() . 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
@Test
public void testToArray() {

    final int cap = 100;
    final BlockingQueue<Integer> dbq = new DisruptorBlockingQueue<Integer>(cap);

    for(int i=0; i<cap; i++) {

        Assert.assertTrue(dbq.offer(Integer.valueOf(i)));
    }

    Object[] objArray = dbq.toArray();
    for(int i=0; i<cap; i++) {
        Assert.assertEquals(objArray[i], Integer.valueOf(i));
    }

}
 
Example 2
@Test
public void testToArray() {

    final int cap = 100;
    final BlockingQueue<Integer> dbq = new PushPullBlockingQueue<Integer>(cap);

    for(int i=0; i<cap; i++) {

        dbq.offer(Integer.valueOf(i));
    }

    Object[] objArray = dbq.toArray();
    for(int i=0; i<cap; i++) {
        Assert.assertEquals(objArray[i], Integer.valueOf(i));
    }

}
 
Example 3
Source Project: HBase.MCC   File: HTableStats.java    License: Apache License 2.0 6 votes vote down vote up
public static long getRollingAvg(BlockingQueue<Long> queue) {
  Long[] times = queue.toArray(new Long[0]);
  
  long totalTime = 0;
  long totalCount = 0;
  
  for (Long time: times) {
    if (time > -1) {
      totalCount++;
      totalTime += time;
    }
  }
  if (totalCount != 0) {
    return totalTime/totalCount;
  } else {
    return 0;
  }
}
 
Example 4
/**
 * Drains the task queue into a new list, normally using drainTo. But if the queue is a DelayQueue or any other kind
 * of queue for which poll or drainTo may fail to remove some elements, it deletes them one by one.
 */
private List<Runnable> drainQueue() {

    BlockingQueue<Runnable> q = workQueue;
    List<Runnable> taskList = new ArrayList<Runnable>();
    q.drainTo(taskList);
    if (!q.isEmpty()) {
        for (Runnable r : q.toArray(new Runnable[0])) {
            if (q.remove(r))
                taskList.add(r);
        }
    }
    return taskList;
}
 
Example 5
Source Project: ThreadDebugger   File: ExceedWait.java    License: Apache License 2.0 5 votes vote down vote up
public List<Runnable> drainExceedQueue() {
    BlockingQueue<Runnable> q = mExceedQueue;
    ArrayList<Runnable> taskList = new ArrayList<>();
    q.drainTo(taskList);
    if (!q.isEmpty()) {
        for (Runnable r : q.toArray(new Runnable[0])) {
            if (q.remove(r))
                taskList.add(r);
        }
    }
    return taskList;
}
 
Example 6
Source Project: disruptor   File: MPMCBlockingQueueTest.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void testToArray() {

    final int cap = 100;
    final BlockingQueue<Integer> dbq = new MPMCBlockingQueue<>(cap);

    for(int i=0; i<cap; i++) {
        Assert.assertTrue(dbq.offer(Integer.valueOf(i)));
    }

    Object[] objArray = dbq.toArray();
    for(int i=0; i<cap; i++) {
        Assert.assertEquals(Integer.valueOf(i), objArray[i]);
    }
}
 
Example 7
Source Project: disruptor   File: MPMCBlockingQueueTest.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void testTypeToArray() {
    final int cap = 100;
    final BlockingQueue<Integer> dbq = new MPMCBlockingQueue<>(cap);

    for(int i=0; i<cap; i++) {
        dbq.offer(Integer.valueOf(i));
    }

    Integer[] t = new Integer[cap];
    dbq.toArray(t);
    for(int i=0; i<cap; i++) {
        Assert.assertEquals(Integer.valueOf(i), t[i]);
    }
}
 
Example 8
@Test
public void testTypeToArray() {
    final int cap = 100;
    final BlockingQueue<Integer> dbq = new DisruptorBlockingQueue<Integer>(cap);

    for(int i=0; i<cap; i++) {
        dbq.offer(Integer.valueOf(i));
    }

    Integer[] t = new Integer[cap];
    dbq.toArray(t);
    for(int i=0; i<cap; i++) {
        Assert.assertEquals(Integer.valueOf(i), t[i]);
    }
}
 
Example 9
@Test
public void testTypeToArray() {
    final int cap = 100;
    final BlockingQueue<Integer> dbq = new PushPullBlockingQueue<Integer>(cap);

    for(int i=0; i<cap; i++) {
        dbq.offer(Integer.valueOf(i));
    }

    Integer[] t = new Integer[cap];
    dbq.toArray(t);
    for(int i=0; i<cap; i++) {
        Assert.assertEquals(Integer.valueOf(i), t[i]);
    }
}
 
Example 10
Source Project: HBase.MCC   File: HTableStats.java    License: Apache License 2.0 5 votes vote down vote up
public static long getRollingMax(BlockingQueue<Long> queue) {
  Long[] times = queue.toArray(new Long[0]);
  
  long maxTime = 0;
  
  for (Long time: times) {
    if (time > maxTime) {
      maxTime = time;
    }
  }
  
  return maxTime;
  
}
 
Example 11
/**
 * Return a summary of the work queue for the operators in this query
 * (non-blocking).
 * 
 * @return A map whose keys are the operator identifiers and whose values
 *         provide summary statistics for the work queue(s) for those
 *         operators.
 *         <p>
 *         Note: For a cluster, there is one work queue per (operator,shard)
 *         pair.
 */
protected Map<Integer/* bopId */, QueueStats> getQueueStats() {
    
    final Map<Integer, QueueStats> map = new HashMap<Integer, QueueStats>();

    for (Map.Entry<BSBundle, BlockingQueue<IChunkMessage<IBindingSet>>> e : operatorQueues
            .entrySet()) {

        final BSBundle bundle = e.getKey();

        final BlockingQueue<IChunkMessage<IBindingSet>> queue = e
                .getValue();

        @SuppressWarnings("unchecked")
        final IChunkMessage<IBindingSet>[] chunks = queue
                .toArray(new IChunkMessage[0]);

        if (chunks.length == 0)
            continue;

        final Integer bopId = Integer.valueOf(bundle.bopId);

        QueueStats stats = map.get(bopId);

        if (stats == null) {

            map.put(bopId, stats = new QueueStats());

        }
        
        stats.shardSet.add(bundle.shardId);
        
        for (IChunkMessage<IBindingSet> msg : chunks) {

            stats.chunkCount++;

            stats.solutionCount += msg.getSolutionCount();

        }

    }

    return map;
    
}