2013-11-14 62 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