账号密码登录
微信安全登录
微信扫描二维码登录

登录后绑定QQ、微信即可实现信息互通

手机验证码登录
找回密码返回
邮箱找回 手机找回
注册账号返回
其他登录方式
分享
  • 收藏
    X
    Java多线程中关于join的问题?
    36
    0

    代码一:

    public static void main(String[] args) throws Exception {
        Thread thread = new Thread(() -> {
          try {
            TimeUnit.SECONDS.sleep(10);
            System.out.println("thread-0 thread exit.");
          } catch (InterruptedException e) {
            e.printStackTrace();
          }
        }, "thread-0");
        thread.start();
        thread.join();
        System.out.println("main thread exit.");
      }

    控制台(10s后打印并退出):

    thread-0 thread exit.
    main thread exit.
    
    Process finished with exit code 0

    代码二:

    public static void main(String[] args) throws Exception {
        Thread.currentThread().join();
        System.out.println("main thread exit.");
      }

    控制台(一直等待中):


    问题:第一段代码是main线程阻塞在thread-0线程上,当thread-0执行完毕,main线程也同时退出,那第二段代码中main线程阻塞在哪个线程上呢,为什么没有退出?

    0
    打赏
    收藏
    点击回答
        全部回答
    • 0
    更多回答
    扫一扫访问手机版
    • 回到顶部
    • 回到顶部