【问题标题】:Does a child thread in Java prevent the parent threads to terminate?Java中的子线程是否会阻止父线程终止?
【发布时间】:2013-11-14 20:50:46
【问题描述】:

当我在另一个线程中创建并启动一个线程时,它会是一个子线程吗?它是否会阻止父线程在子线程运行时终止?例如:

new Thread(new Runnable() {
    @Override
    public void run() {
        //Do Sth
        new Thread(new Runnable() {
            @Override
            public void run() {
                //Do Sth
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        //Do Sth
                    }
                }).start();
                //Do Sth
            }
        }).start();
        //Do Sth
    }
    //Do Sth            
}).start();

【问题讨论】:

  • 我认为你问错了问题。查看 setDemon 在 Java 中的作用。
  • 我已经搜索过该主题!我问它是因为我认为它对其他人也有用,另一方面,我对线程的心态有误! @SotiriosDelimanolis

标签: java multithreading


【解决方案1】:

在您的代码中,对象之间存在父子关系。但是,线程之间没有父子关系的概念。一旦这两个线程运行,它们基本上就是对等的。

假设线程 A 启动线程 B。除非它们之间有显式同步,否则任何一个线程都可以随时终止,而不会影响另一个线程。

请注意,只要线程存在,任何一个线程都可以使 进程 保持活动状态。见What is Daemon thread in Java?

【讨论】:

【解决方案2】:

当我在另一个线程中创建和启动一个线程时,它会是一个子线程吗?

Java 没有“子”线程的真正概念。当您启动一个线程时,它会从“父”继承守护进程和优先级,但这就是父/子关系的结束。

// in Thread.init()
this.daemon = parent.isDaemon();
this.priority = parent.getPriority();

当线程启动时,它沿着它的“父”运行,两者之间没有联系。

在子线程运行时是否阻止父线程终止?

不,它没有。但是我怀疑您真的想知道线程是否可以阻止 应用程序 的终止。如果子线程是非守护线程,那么即使主线程(也是非守护线程)完成,您的应用程序也会等待子线程完成。根据定义,JVM 将等待所有非守护线程完成。一旦最后一个非守护线程完成,JVM 就可以终止。

见:What is Daemon thread in Java?

【讨论】:

    【解决方案3】:

    会变成子线程,但不会阻止父线程终止。

    【讨论】:

      【解决方案4】:

      两个线程中没有父子关系。 ParentThread 完成后,ParentThread 不会等待 ChildThread.

      public class MainThread {
          public static void main(String[] args) {
               ParentThread pt=new ParentThread();
               pt.start();
               for(int i=1;i<=20;i++){
                   System.out.println("ParentThread alive: "+pt.isAlive());
                   try{
                       Thread.currentThread().sleep(500);
                   }catch(Exception ex){
                       ex.printStackTrace();
                   }
               }          
           }
       }
      
      class ParentThread extends Thread{
          public void run(){
              ChildThread ct=new ChildThread();
              ct.start();
              for(int i=1;i<=10;i++){
                  try{
                      System.out.println("ChildThread alive: "+ ct.isAlive() + ", i = "+i);
                      sleep(500);
                  }catch(Exception ex){
                      ex.printStackTrace();
                  }
              }   
          } 
      }
      class ChildThread extends Thread{
          public void run(){
              for(int i=1;i<=20;i++){
                  try{
                      System.out.println("ChildThread i = "+i);
                      sleep(500);
                  }catch(Exception ex){
                      ex.printStackTrace();
                  }
              }
          }
      }
      

      输出(喜欢):

      ParentThread alive: true
      ChildThread alive: true, i = 1
      ChildThread i = 1
      ParentThread alive: true
      ChildThread i = 2
      ChildThread alive: true, i = 2
      ParentThread alive: true
      ChildThread i = 3
      ChildThread alive: true, i = 3
      ParentThread alive: true
      ChildThread i = 4
      ChildThread alive: true, i = 4
      ParentThread alive: true
      ChildThread i = 5
      ChildThread alive: true, i = 5
      ParentThread alive: true
      ChildThread i = 6
      ChildThread alive: true, i = 6
      ParentThread alive: true
      ChildThread alive: true, i = 7
      ChildThread i = 7
      ParentThread alive: true
      ChildThread i = 8
      ChildThread alive: true, i = 8
      ParentThread alive: true
      ChildThread alive: true, i = 9
      ChildThread i = 9
      ParentThread alive: true
      ChildThread alive: true, i = 10
      ChildThread i = 10
      ParentThread alive: true
      ChildThread i = 11
      ParentThread alive: false
      ChildThread i = 12
      ChildThread i = 13
      ParentThread alive: false
      ParentThread alive: false
      ChildThread i = 14
      ParentThread alive: false
      ChildThread i = 15
      ParentThread alive: false
      ChildThread i = 16
      ParentThread alive: false
      ChildThread i = 17
      ChildThread i = 18
      ParentThread alive: false
      ParentThread alive: false
      ChildThread i = 19
      ParentThread alive: false
      ChildThread i = 20
      

      【讨论】:

        猜你喜欢
        • 2011-06-07
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-11-18
        • 2014-04-14
        • 1970-01-01
        相关资源
        最近更新 更多