Java : a method to do multiple calculations on arrays quickly

扶醉桌前 提交于 2019-12-08 13:34:33

问题


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

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.

My current idea comes from this multi-threading example

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?


回答1:


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()

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()



来源:https://stackoverflow.com/questions/24920392/java-a-method-to-do-multiple-calculations-on-arrays-quickly

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!