Question: What is a way to simply wait for all threaded process to finish? For example, let's say I have:

```
public class DoSomethingInAThread implements Runnable{

    public static void main(String[] args) {
        for (int n=0; n<1000; n++) {
            Thread t = new Thread(new DoSomethingInAThread());
            t.start();
        }
        // wait for all threads' run() methods to complete before continuing
    }

    public void run() {
        // do something here
    }

}

```

How do I alter this so the `main()` method pauses at the comment until all threads' `run()` methods exit? Thanks!

Answer: 
Avoid the Thread class altogether and instead use the higher abstractions provided in java.util.concurrent

The ExecutorService class provides the [method invokeAll](http://java.sun.com/javase/6/docs/api/java/util/concurrent/ExecutorService.html#invokeAll(java.util.Collection)) that seems to do just what you want.
Create the thread object inside the first for loop.

```
for (int i = 0; i < threads.length; i++) {
     threads[i] = new Thread(new Runnable() {
         public void run() {
             // some code to run in parallel
         }
     });
     threads[i].start();
 }

```

And then so what everyone here is saying.

```
for(i = 0; i < threads.length; i++)
  threads[i].join();

```