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.
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?
解决方案
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()