/* package whatever; // don't place package name! */

public class Main {

    private static Thread criarThread(final int numero) {
        return new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    System.out.println("t" + numero + " começou");
                    Thread.sleep((int) (Math.random() * 10000));
                    System.out.println("t" + numero + " terminou");
                } catch (InterruptedException e) {
                    // Ignora...
                }
            }
        });
    }

	public static void main(String[] args) {
        Thread[] ts = {
            criarThread(1),
            criarThread(2),
            criarThread(3),
            criarThread(4),
            criarThread(5)
        };
        for (Thread t : ts) {
            t.start();
        }

        new Thread(new Runnable() {
            @Override
            public void run() {
                for (Thread t : ts) {
                    try {
                        t.join();
                    } catch (InterruptedException e) {
                // Ignora...
                    }
                }
                System.out.println("Todas as threads terminaram");
            }
        }).start();
        System.out.println("A main está livre");
	}
}