问题描述
我有一个Queue
,我想将其转换为long[]
并将其传递给我的计算百分位数的方法.
I have a Queue
which I want to convert into long[]
and pass it to my method which calculate percentiles.
private final ConcurrentLinkedQueue<Long> holder = new ConcurrentLinkedQueue<>();
我之所以使用ConcurrentLinkedQueue
,是因为我将多线程应用程序中的延迟(以毫秒为单位)插入到上面的holder
队列中,所以我想保持线程安全.
I am using ConcurrentLinkedQueue
because I am inserting latencies which are in milliseconds from multithread application into my above holder
queue so I wanted to be thread safe.
现在我的问题是如何将holder
队列转换为long[]
长数组,以便可以将其传递给下面的方法?有什么办法吗?
Now my question is how can I convert holder
queue into long[]
long array so that I can pass it to my below method? Is there any way to do that?
public static long[] percentiles(long[] latencies, double... percentiles) {
Arrays.sort(latencies, 0, latencies.length);
long[] values = new long[percentiles.length];
for (int i = 0; i < percentiles.length; i++) {
int index = (int) (percentiles[i] * latencies.length);
values[i] = latencies[index];
}
return values;
}
推荐答案
它看起来像 ConcurrentLinkedQueue#toArray(T [] a)将为您提供90%的解决方案:
It looks like ConcurrentLinkedQueue#toArray(T[] a) will get you 90% of the way there:
Long[] longs = holder.toArray(new Long[0]);
将剩下的Long[]
转换为long[]
作为学生的练习. ;-)
Converting Long[]
to long[]
left as exercise to student. ;-)
这篇关于将队列转换成长数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!