Q:

how do I set up the following thread in Java?

I have a thread with the following form:

each execution of each thread is supposed to run a function in the class. That function is completely safe to run by itself. The function returns a value, say an int.
After all threads have been executed, the function values need to be accumulated.

So, it goes (in pseudo-code) something like that:
  a = 0
  for each i between 1 to N
      spawn a thread independently and call the command v = f(i)
      when thread finishes, do safely: a = a + v
  end

I am not sure how to use Java in that case.
The problem is not creating the thread, I know this can be done using 
new Thread() { 
   public void run() { 
     ... 
   } 
} 

the problem is accumulating all the answers.
Thanks for any info.

A:

I would probably do something like:
 public class Main {
     int a = 0;
     int[] values;
     int[] results;

     public Main() {
         // Init values array

         results = new int[N];
     }

     public int doStuff() {
         LinkedList<Thread> threads = new LinkedList<Thread>();

         for (final int i : values) {
             Thread t = new Thread() {
                 public void run() {
                     accumulate(foo(i));
                 }
             };

             threads.add(t);
             t.start();
          }

          for (Thread t : threads) {
              try {
                  t.join();
              } catch (InterruptedException e) {
                  // Act accordingly, maybe ignore?
              }
          }

          return a;
     }

     synchronized void accumulate(int v) {
          // Synchronized because a += v is actually
          //    tmp = a + v;
          //    a = tmp;
          // which can cause a race condition AFAIK
          a += v;
     }
 }

A:

Use an ExecutorCompletionService, Executor, and Callable.:
Start with a Callable that calls your int function:
public class MyCallable implements Callable<Integer> {
    private final int i;

    public MyCallable(int i) {
        this.i = i;
    }

    public Integer call() {
        return Integer.valueOf(myFunction(i));
    }
}

Create an Executor:
private final Executor executor = Executors.newFixedThreadPool(10);

10 is the maximum number of threads to execute at once.
Then wrap it in an ExecutorCompletionService and submit your jobs:
CompletionService<Integer> compService = new ExecutionCompletionService<Integer>(executor);

// Make sure to track the number of jobs you submit
int jobCount;
for (int i = 0; i < n; i++) {
    compService.submit(new MyCallable(i));
    jobCount++;
}

// Get the results
int a = 0;
for (int i = 0; i < jobCount; i++) {
    a += compService.take().get().intValue();
}

ExecutorCompletionService allows you to pull tasks off of a queue as they complete. This is a little different from joining threads. Although the overall outcome is the same, if you want to update a UI as the threads complete, you won't know what order the threads are going to complete using a join. That last for loop could be like this:
for (int i = 0; i < jobCount; i++) {
    a += compService.take().get().intValue();
    updateUi(a);
}

And this will update the UI as tasks complete. Using a Thread.join won't necessarily do this since you'll be getting the results in the order that you call the joins, not the order that the threads complete.
Through the use of the executor, this will also allow you to limit the number of simultaneous jobs you're running at a given time so you don't accidentally thread-bomb your system.

