【多线程例题】 在子线程执行完毕后再执行主线程代码

在子线程执行完毕后再执行主线程代码

【多线程例题】 在子线程执行完毕后再执行主线程代码_第1张图片

public static void main(String[] args) throws InterruptedException {
        Thread[] threads = new Thread[20];
        for(int i=0; i<20; i++){
            final int n = i;
            threads[i] = new Thread(new Runnable() {
                @Override
                public void run() {//内部类使用外部的变量,必须是final修饰
                    System.out.println(n);
                }
            });
        }
        for(Thread t : threads){
            t.start();
        }
        for(Thread t : threads){//同时执行20个线程,再等待所有线程执行完毕
            t.join();
        }
        System.out.println("OK");
    }

你可能感兴趣的:(JavaEE初阶,算法)