2013-11-14 222 views
6

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

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(); 
+0

我想你问错了问题。在Java中查找setDemon的功能。 –

+0

在这里看看这个问题:http://stackoverflow.com/questions/9939076/wait-until-child-threads-completed-java – mwhs

+0

我已经搜索了关于这个话题!我问了这个问题,因为我认为这对其他人也是有用的,另一方面,在我对线程的看法上我错了。 @SotiriosDelimanolis – Johnny

回答

11

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

让我们假设线程A启动线程B.除非它们之间有明确的同步,否则任何线程都可以随时终止而不会影响其他线程。

请注意,只要线程处于活动状态,任一线程都可以使进程保持活动状态。请参阅What is Daemon thread in Java?

+0

在我看来,创建线程是嵌套的。他们之间有一个父/子关系,尽管他们共享相同的上下文类加载器没什么意义。请参阅http://docs.oracle.com/javase/7/docs/api/java/lang/Thread.html#getContextClassLoader%28%29 – Changgeng

0

它将成为子线程,但它不会阻止父线程终止。

3

当我在另一个线程内创建并启动线程时,它会成为子线程吗?

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

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

当线程启动时,它沿着它的“父”方运行,并且两者之间没有链接。

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

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

请参见:What is Daemon thread in Java?

-1

中有两个线程没有父子关系。 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