【问题标题】:How can I add an Array into a Queue?如何将数组添加到队列中?
【发布时间】:2017-05-19 01:12:06
【问题描述】:

是否可以将数组专门添加到ConcurrentLinkedQueue 中?如果是这样,.add 语句究竟会是什么样子?

这是与家庭作业相关的,但是,我的整个程序旨在对 MyObjects 执行计算(MyObjects 的默认构造函数会生成随机值来执行大量计算)。

例如:

//Note: I couldn't use the Diamond Braces for the Queue-type when posing this question

ConcurrentLinkedQueue theLinkedQueue {MyObjects[]} = 
                 new ConcurrentLinkedQueue{MyObjects[]}();

MyObjects[] theArray = null;

for(int i = 0; i < 100; i++){
   theArray = new MyObjects[CONSTANT_SIZE]; 
   theLinkedQueue.add(theArray(objparameter1, objparameter2));
}

该程序实现了多线程,在我的线程类中,我将Queue 传递到constructor,并试图取出MyObject 数组,其中@987654329 @ temp 会指向,但到目前为止,我只能一次向我的Queue 添加一个MyObject 并拉动它。我希望能够添加相同数量的 MyObjects 作为单个组件,而不是单独添加。我已经为 NetBeans IDE 尝试了各种代码行来推荐一种抛出UnsupportedOperation Exception 的方法。如何将数组添加到我的ConcurrentLinkedQueue

(如果我的问题很密集或令人困惑,也很抱歉,这是第一次在这里发布)。

【问题讨论】:

    标签: java arrays multithreading queue


    【解决方案1】:

    声明队列的正确语法是:

    ConcurrentLinkedQueue<MyObjects> theLinkedQueue = new ConcurrentLinkedQueue<>();
    

    从那里开始,看看事情会如何发展。

    【讨论】:

      【解决方案2】:

      我找到了解决方案,即简单地添加不包含“objparameters”的数组。

      theLinkedQueue.add(a);  //where a was a 'MyObject' array.
      

      我假设您必须为每个数组索引加载要传递的参数,这看起来很愚蠢。

      【讨论】:

        猜你喜欢
        • 2017-07-03
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-11-20
        • 1970-01-01
        • 2015-09-27
        相关资源
        最近更新 更多