将队列转换成长数组? [英] Convert queue into long array?

查看:96
本文介绍了将队列转换成长数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个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;
  }

推荐答案

它看起来像

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. ;-)

这篇关于将队列转换成长数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆