11

Here are my two classes:

public class Firstclass {
    public static void main(String args[]) throws InterruptedException {
        System.out.println("Main start....");
        Secondclass t1 = new Secondclass();
        t1.setName("First Thread");
        Secondclass t2 = new Secondclass();
        t2.setName("Second Thread");
        t1.start();
        t2.start();
        System.out.println("Main close...");
    }
}

and

public class Secondclass extends Thread {
    @Override
    public void run() {
        try {
            loop();
        } catch(Exception e) {
            System.out.println("exception is" + e);
        }
    }

    public void loop() throws InterruptedException {
        for(int i = 0; i <= 10; i++) {
            Thread t = Thread.currentThread();
            String threadname = t.getName();
            if(threadname.equals("First Thread")) {
                Thread.sleep(1000);
            } else {
                Thread.sleep(1500);
            }
            System.out.println("i==" + i);   
        }   
    }    
}

Now when I run Firstclass then the output is:

Main start....
Main close...
i==0
i==0
i==1
i==1
i==2
i==3
i==2
i==4
i==3
i==5
i==6
i==4
i==7
i==5
i==8
i==9
i==6
i==10
i==7
i==8
i==9
i==10

My question is: Let's consider main method is executed by a thread 'T' in JVM and t1 and t2 are obviously child threads of parent thread T so when T thread that is the main method dies how can T1 and T2 still execute and give us output. Because if parent thread dies or terminates then child thread should also die or terminate.

YoussefDir
  • 287
  • 1
  • 3
  • 16
TruePS
  • 493
  • 2
  • 12
  • 33
  • If you run those threads as daemon threads, when main thread finishes, your application will exit. Have a look at "daemon" threads. JVM will exit when the only running threads are daemon threads. `The Java Virtual Machine exits when the only threads running are all daemon threads. ` – anonymous Mar 04 '14 at 05:50
  • The whole point of multi-threading is to get simultaneous action. The main thread and the other threads will rotate in execution. If you called the .join() method on your threads, that will ensure that the other threads finish executing before the main thread continues. – Solace Mar 04 '14 at 05:52
  • @ SOlace and @anonymous I know the difference between daemon thread and user threads but let's avoid that for now is how come child threads executing even if parent thread dies? – TruePS Mar 04 '14 at 06:00
  • 1
    Please remove all traces of that mtu link from your machine and burn all hard copy. It is so bad that it's beyond the telling of it. – Martin James Mar 04 '14 at 06:53
  • @Martin So you are saying that even if parent thread that is main thread exits child thread still executes.If that is what you are implying then plz provide source of your answer.If you can provide link from oracle.docs that would be awesome. – TruePS Mar 04 '14 at 07:09
  • @Martin: Updated my answer. This paper is not about Java. Therefore differences are definitly there. – wumpz Mar 04 '14 at 08:47
  • Indeed, it's not just wrong in Java. It's wrong in many other languages/OS too. – Martin James Mar 04 '14 at 11:24

3 Answers3

18

There is no notion of a parent-child relationship between threads. Once the two threads are running they're basically peers. The main thread can exit while other thread still running.

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.

Abimaran Kugathasan
  • 31,165
  • 11
  • 75
  • 105
  • I was reading this article (http://www.cs.mtu.edu/~shene/NSF-3/e-Book/FUNDAMENTALS/thread-management.html) they have said that 'if the parent thread terminates, all of its child threads terminate as well'.So what is your opinion now and can you provide source of your answer. – TruePS Mar 04 '14 at 05:51
  • 5
    Remove that link from your favourites. If you have hard copy, burn it. The 'information' contained will do you more harm than good:( – Martin James Mar 04 '14 at 06:34
  • @MArtinJames, Hope you have responded to Question OP, not to me! – Abimaran Kugathasan Mar 04 '14 at 06:36
  • @Kugathasan So you are saying that even if parent thread that is main thread exits child thread still executes.If that is what you are implying then plz provide source of your answer.If you can provide link from oracle.docs that would be awesome. – TruePS Mar 04 '14 at 06:40
  • @Martin So you are saying that even if parent thread that is main thread exits child thread still executes.If that is what you are implying then plz provide source of your answer.If you can provide link from oracle.docs that would be awesome. – TruePS Mar 04 '14 at 06:40
  • @KugathasanAbimaran - oh yes. OP, and that apalling link to so much 'economical with the truth', and downright incorrect, info contained. – Martin James Mar 04 '14 at 06:41
  • In fact, @KugathasanAbimaran, have some upboats:) – Martin James Mar 04 '14 at 06:45
  • @ Axel okay my bad but if you can give me answer that would help me. – TruePS Mar 04 '14 at 08:11
  • @TruePS what are you trying to do? You have gotten the answers to your question here – Karthik T Mar 04 '14 at 08:21
  • @karthik I have got 2 answers here some are saying that main method dies() and other threads are still running and some are saying that main method() dies when it's child threads dies so which one is right? – TruePS Mar 04 '14 at 08:25
  • @TruePS http://stackoverflow.com/questions/5642802/termination-of-program-on-main-thread-exit talks about the same thing as this answer and points to oracle docs – Karthik T Mar 04 '14 at 08:28
  • @TruePS It appears that this is the primary difference betw user threads and daemon threads - http://stackoverflow.com/questions/2213340/what-is-daemon-thread-in-java – Karthik T Mar 04 '14 at 08:30
  • 1
    @karthik I have seen that but I didn't get my answer – TruePS Mar 04 '14 at 08:31
4

Java does not know the concept of child threads. The three of your threads are treated equally. What comes next to your thinking is the creation of daemon threads, that are terminated when the last java non daemon thread is stopped.

The paper you posted is about a learning system called ThreadMentor. But this system behaves different from Java, this is not about Java.

Here could be a good start about Javas threading model:

http://docs.oracle.com/javase/tutorial/essential/concurrency/

wumpz
  • 8,257
  • 3
  • 30
  • 25
  • I was reading this article (cs.mtu.edu/~shene/NSF-3/e-Book/FUNDAMENTALS/…) they have said that 'if the parent thread terminates, all of its child threads terminate as well'.So what is your opinion now and can you provide source of your answer? – TruePS Mar 04 '14 at 05:57
  • sorry the link is [link] http://www.cs.mtu.edu/~shene/NSF-3/e-Book/FUNDAMENTALS/thread-management.html. – TruePS Mar 04 '14 at 06:51
  • 1
    Daemon threads are not stopped when the main thread terminates. They are stopped when the last non-daemon thread terminates. The main thread is a non-daemon thread, but it is not necessarily the last one to terminate. – Solomon Slow Mar 04 '14 at 18:55
  • Sure you are right. I was referring to his example but generalised it now. Thx – wumpz Mar 05 '14 at 12:02
0

In this application, the main thread started first and is running even though you have started the t1 and t2 they enter into ready state but they are not running, the current thread running is main thread. Once main thread completes all its statements it did not die immediately, it waits until it completes its child threads execution. as it happened in the above case.

sai
  • 21
  • 1
  • 2