join示例分析

public class TestJoin {

    public static void main(String[] args) throws InterruptedException {
        System.err.println(Thread.currentThread().getName()+"...start");
        JoinThread t1 = new JoinThread("t1");
        t1.start();

        System.err.println(Thread.currentThread().getName()+"...t1.join... wait t1 died.");
        t1.join();//让t1加入到当前线程main,然后main线程进入阻塞状态,直到t1执行run完成

        System.err.println(Thread.currentThread().getName()+"....end");
    }


    static class JoinThread extends Thread {
        public JoinThread(String name) {
            super(name);
        }
        @Override
        public void run() {
            System.err.println(Thread.currentThread().getName()+"....run start");
            for (int i = 0; i < 5; i++) {
                try {
                    System.err.println(Thread.currentThread().getName()+"..."+i);
                    Thread.sleep(500);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            System.err.println(Thread.currentThread().getName()+"........run end");
        }
    }
}

 

相关文章:

  • 2021-08-14
  • 2022-12-23
  • 2022-12-23
  • 2021-12-20
  • 2021-08-22
  • 2021-06-11
  • 2022-12-23
  • 2021-09-12
猜你喜欢
  • 2022-12-23
  • 2021-09-21
  • 2021-12-09
  • 2022-01-24
  • 2021-07-20
  • 2021-12-21
  • 2022-01-22
相关资源
相似解决方案