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: 
Depending on your needs, you may also want to check out the classes CountDownLatch and CyclicBarrier in the java.util.concurrent package. They can be useful if you want your threads to wait for each other, or if you want more fine-grained control over the way your threads execute (e.g., waiting in their internal execution for another thread to set some state). You could also use a CountDownLatch to signal all of your threads to start at the same time, instead of starting them one by one as you iterate through your loop. The standard API docs have an example of this, plus using another CountDownLatch to wait for all threads to complete their execution.
You can do it with the Object ["ThreadGroup" and its parameter activeCount](https://docs.oracle.com/javase/8/docs/api/java/lang/ThreadGroup.html#activeCount--):