10

When I create and start a thread inside another thread, will it be a child thread? Does it prevent the termination of the parent thread while the child thread is running? For example:

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();
Gray
  • 115,027
  • 24
  • 293
  • 354
Johnny
  • 1,509
  • 5
  • 25
  • 38
  • I think you're asking the wrong question. Look up what setDemon does in Java. – Alan Stokes Nov 14 '13 at 20:53
  • Look at this question here: http://stackoverflow.com/questions/9939076/wait-until-child-threads-completed-java – mwhs Nov 14 '13 at 20:55
  • I had searched about the topic! I asked it because I think that it will be also useful to others and on the other hand I get where I'm wrong in my mentality about the threads! @SotiriosDelimanolis – Johnny Nov 14 '13 at 21:17

4 Answers4

15

In your code, there is a parent-child relationship between objects. However, there is no notion of a parent-child relationship between threads. Once the two threads are running they're basically peers.

Let's say that thread A starts thread B. Unless there's explicit synchronisation between them, either thread can terminate whenever it pleases without affecting the other thread.

Note that either thread can keep the process alive for as long as the thread lives. See What is Daemon thread in Java?

Community
  • 1
  • 1
NPE
  • 486,780
  • 108
  • 951
  • 1,012
  • In my mind the threads creation are nested. There's a parent/child relationship between them although they doesn't make much sense they do share the same context class loader. See http://docs.oracle.com/javase/7/docs/api/java/lang/Thread.html#getContextClassLoader%28%29 – Changgeng Nov 14 '13 at 21:10
  • There is a concept of child threads in the context of ThreadLocal: https://docs.oracle.com/javase/8/docs/api/java/lang/InheritableThreadLocal.html – shmosel Oct 02 '22 at 21:04
7

When I create and start a thread inside another thread, will it be a child thread?

Java has no real concept of "child" threads. When you start a thread it inherits the daemon and priority from the "parent" but that's the end of the parent/child relationship.

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

When the thread starts it runs along side its "parent" and there is no linkage between the two.

Does it prevent the termination of the parent thread while the child thread is running?

No it does not. However I suspect that you are really wondering if a thread can prevent the termination of the application. If the child thread is non-daemon then even if the main thread (which is also non-daemon) finishes, your application will wait for the child thread to finish. By definition, the JVM will wait for all non-daemon threads to finish. Once the last non-daemon thread finishes, the JVM can terminate.

See: What is Daemon thread in Java?

Community
  • 1
  • 1
Gray
  • 115,027
  • 24
  • 293
  • 354
0

It will become a child thread, but it won't prevent parent thread from termination.

Changgeng
  • 578
  • 2
  • 8
0

There is no parent-child relationship in two threads. After Completion of ParentThread, ParentThread does not waiting for a 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();
            }
        }
    }
}

output (like):

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
sparkitny
  • 1,503
  • 4
  • 18
  • 23
Rakesh Kr
  • 41
  • 1
  • 3