下面列出了java.util.concurrent.BlockingQueue#toArray ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@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));
}
}
@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));
}
}
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;
}
}
/**
* 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;
}
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;
}
@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]);
}
}
@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]);
}
}
@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]);
}
}
@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]);
}
}
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;
}
/**
* 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;
}