0

I was reading Multithreading from The Complete Reference and then I was struck at this code,I am unable to understand the output of this code.Can somebody help me with this?

class NewThread implements Runnable
{ 
    Thread t;
    NewThread() 
    {
        t = new Thread(this, "Demo Thread"); 
        System.out.println("Child thread: " + t);
        t.start(); 
    }
    public void run() 
    { 
        try 
        { 
            for(int i = 5; i > 0; i--) 
            { 
                System.out.println("Child Thread: " + i);
                Thread.sleep(500);
            }
        }
        catch (InterruptedException e) 
        { 
            System.out.println("Child interrupted.");
        }
        System.out.println("Exiting child thread.");
    }
}
class First
{
    public static void main(String args[]) 
    {
        new NewThread();
        try 
        { 
            for(int i = 5; i > 0; i--) 
            {
                 System.out.println("Main Thread: " + i); 
                 Thread.sleep(1000);
            }
        }
        catch (InterruptedException e) 
        {
             System.out.println("Main thread interrupted.");
        }
        System.out.println("Main thread exiting.");
    }
}

It produces the output:

Child thread: Thread[Demo Thread,5,main]

Main Thread: 5

Child Thread: 5

Child Thread: 4

Main Thread: 4

Child Thread: 3

Child Thread: 2

Main Thread: 3

Child Thread: 1

Exiting child thread

Main Thread: 2

Main Thread: 1

Main thread exiting

From the main() method,the NewThread() constructor is called and then an instance of Thread class is created named "demo thread" and the first print() statement gets executed.After that the start() method is called.Is this start method not supposed to call run() method implicitly and so the child loop should be executed,but according to the output the control goes in the main loop.How can the control go to the main() loop,even if we are calling t.start()?Can somebody please clarify the code output to me please?

3
  • 1
    Note that this uses a nonstandard spacing standard and is difficult to read; this layout (especially putting opening braces on their own line) isn't something to emulate. Commented Jul 12, 2015 at 8:10
  • @chrylis-Is is fine now? Commented Jul 12, 2015 at 8:14
  • The whole points of threads is to make things execute concurrently. Starting a thread executes the run() method, in another thread, which executes in parallel to the main thread. The main thread doesn't wait for the new thread to be completed, otherwise, starting a thread would be useless. Commented Jul 12, 2015 at 8:16

2 Answers 2

3

Once start() is called, there are now two threads running simultaneously. start() returns immediately and the main loop continues (one loop very 1000mS). But the child loop is now running at the same time also - one loop every 500mS. So, until each loop finishes, there will be two child lines printed for every main line.

Sign up to request clarification or add additional context in comments.

Comments

1

Unless there's a happens-before relationship, the order of execution of independent threads is nondeterministic; this is what makes concurrency challenging. After t.start() is called, there's no relationship at all between the main thread and the thread in t, and in the unlikely case where the system is heavily loaded, one thread or the other could theoretically complete all in sequence before control returns to the other thread at all.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.