Java + 线程系列之join(七)

 private static void showJoin() throws InterruptedException {
        Thread t1 = new Thread(new Runnable() {
            @Override
            public void run() {
                for (int i = 0; i < 10; i++) {
                    System.out.println(i);
                    try {
                        Thread.sleep(200);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        });
        t1.start();

        //join 方法
         t1.join();  //t1.join() 等待t1执行完,其他线程再执行

        //t1.join() 等待t1执行完,其他线程再执行
        //t1.join(500)等待t1执行500毫秒后,其他线程再执行
       // t1.join(500);
        System.out.println("主线程执行完毕了");
    }

运行结果:Java + 线程系列之join(七)_第1张图片

你可能感兴趣的:(java,线程)