Java:一种快速对数组进行多次计算的方法 [英] Java : a method to do multiple calculations on arrays quickly

查看:302
本文介绍了Java:一种快速对数组进行多次计算的方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

很抱歉,如果这不是这些问题的正确答案,但我需要一些基本帮助.

Sorry if this isn't the right place for these questions, but I'm in need of some base help.

我有一个名为Differential的类,该类具有成员变量mValues(List>).我要做的是遍历所有值并将它们相互比较.这意味着对于五个值,我正在进行10个比较.但是,这将至少用于20,000个列表.我要进行三个计算,我想知道应该如何处理.

I have a class called Differential and that has a member variable mValues (List>). What I want to do is iterate through all the values and compare them with each other. This means for five values I am doing 10 comparisons. However this is going to be used for at least 20,000 lists. There are three calculations that I want to do and I was wondering how I should approach this.

我当前的想法来自此多线程示例

我当时以为我会使用完成服务来遍历多线程中的值.然后,每个线程将处理多个计算并返回它们.这看起来合适吗?有没有一种我没有意识到的快速完成此操作的方法?

I was thinking that I would use a completionService to iterate through the values in multi-threading. Then each thread would deal with the multiple calculations and return them. Does this seem appropriate? Is there a way of doing this somewhat quickly that I'm not realizing?

推荐答案

在创建多线程版本之前,我建议使单线程版本正常工作并向我们展示代码.然后,您可以轻松拆分for循环(序列)的工作.下面省略了许多细节.

Before you make the multi-thread version, I would suggest making the single thread version work and show us the code. Then you can split the work of a for loop (sequence) easily. Lots of details omitted below.

mysequence = ...//array or list
//2 threads
c1=new Callable(){
Object call(){
  for(i=0...mysequence.size()/2)
     //do work
   }
return result;
}
c2=new Callable(){
Object call(){
  for(i=mysequence.size()/2+1 ... mysequence.size()-1)
     //do work
   }
return result;
}
ExecutorService exec=Executors.xxxThreadPool(2);
fut1= exec.submit(c1)
fut2 =exec.submit(c2)

fut1.get()
fut2.get()

让我找到我的另一篇文章的链接.在这里查看我的答案以获得更好的示例. 线程不会在结束时自然退出run()

Let me find a link to another post of mine. See my answer here for a better example. Thread won't naturally exit at end of run()

这篇关于Java:一种快速对数组进行多次计算的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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