0

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?

10
  • If you are allowed to use Java 8 then it might be worth having a look on the new lambda expressions Commented Jul 23, 2014 at 20:33
  • How would I go about doing that? I am able to use java 8 Commented Jul 23, 2014 at 20:34
  • Do you have to perform all the comparisons? Meaning, can the final point you want to reach be derived by performing a sub-set of comparisons? Commented Jul 23, 2014 at 20:36
  • To be able to use parallelity with lambdas and stream operations the first step is to formulate the problem in a way where the single steps can be parallelized. Then each step has to be implemented as some map or reduce task. But to be able to do that one should have at least some basic experience with lambdas and streams in java Commented Jul 23, 2014 at 20:37
  • 3
    Your question is really broad -- it's basically "how does concurrency work?" -- and you don't tell us how or why you are comparing values in the list. I think a concrete example (in code) of what you are doing is in order. Commented Jul 23, 2014 at 20:50

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.