10

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

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();
4

4 回答 4

15

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

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

请注意,只要线程存在,任何一个线程都可以使进程保持活动状态。请参阅Java 中的守护线程是什么?

于 2013-11-14T20:55:45.590 回答
6

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

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

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

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

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

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

请参阅:Java 中的守护线程是什么?

于 2013-11-14T23:54:33.200 回答
0

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

于 2013-11-14T20:54:22.767 回答
0

两个线程中没有父子关系。完成后ParentThreadParentThread不等了 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
于 2018-01-23T08:27:53.493 回答