-1

As I was learning multithreading in java, i came to know that there is no execution order for threads. As per my understanding is below statement true?
A user thread (which is not a Daemon Thread) it should terminate before termination of main thread.

I have read similar links:
if main method completes the execution, what happens to any long running thread?
When does the main thread stop in Java?

I have a program to demontrate, please correct me.

class ThreadDemo {  
  public static void main(String args[]) {      
    Thread t = new Thread(new Runnable(){  
            @Override  
            public void run() {  
                System.out.println("Within 'Child Thread' @ "+System.currentTimeMillis());                  
            }         
    }, "Child Thread");   

    //t.setDaemon(false);  
    t.start();  
    System.out.println(Thread.currentThread()+" thread is alive:"+Thread.currentThread().isAlive());  
    System.out.println(t+" thread is alive:"+t.isAlive());  

    System.out.println("'Main' thread exiting @ "+System.currentTimeMillis());     
  }  
}

Most of the times output to this program on my system is

'Main' thread exiting @ 1406971862950
Within 'Child Thread' @ 1406971862952

Does this mean that main thread exits before child thread? If yes, then why is this happening?

Community
  • 1
  • 1
Vinit
  • 21
  • 4
  • 3
    What makes you think the "user thread" needs to terminate first, or that it *will* terminate first in your code? – Jon Skeet Aug 02 '14 at 11:52
  • Have you read this before asking?http://stackoverflow.com/questions/8784333/can-the-main-thread-die-before-the-child-thread – wliao Aug 02 '14 at 12:01
  • possible duplicate of [When does the main thread stop in Java?](http://stackoverflow.com/questions/7416018/when-does-the-main-thread-stop-in-java) – fabian Aug 02 '14 at 12:11
  • Yes, I have gone through nearly all the possible links. But as is written in most of the books that, main thread or daemon threads terminate at end. So why is this child thread terminating at last, i.e before main thread ? – Vinit Aug 02 '14 at 13:23
  • "A [non-daemon thread] should terminate before termination of main thread." Not True! Here's what is true: The JVM will kill all of the daemon threads (if any), and then shut down when there are no non-daemon threads left. The main thread is a non-daemon thread, and so is any thread that you start() without first calling setDaemon(true). If the main() thread creates other threads and then exits, there's nothing wrong with that. The program will keep running so long as at least one of the other (non-daemon) threads is still running. – Solomon Slow Aug 02 '14 at 15:14
  • Thanks @james for making things much clearer. – Vinit Aug 03 '14 at 10:59
  • "*A user thread (which is not a Daemon Thread) it should terminate before termination of main thread.*" If you know this, why are you writing the code shown in the question? Do you just want to find out what happens if you don't do what you know you should do? – David Schwartz Aug 04 '14 at 10:47

2 Answers2

0

That's true. When you execute your program, a main thread is created which executes the main method. Since you are creating another thread in main, both the created thread and main thread will execute parallerly irrespective of each other.

However if you require your created thread to complete executing and than main finishing the execution jst add followind code after starting your thread.

t.join(); 

This would force your main thread to stop until your created thread finishes execution.

Darshan Lila
  • 5,772
  • 2
  • 24
  • 34
  • Thanks, I accept but if i don't use `t.join();` shouldn't `'main'` thread terminate before `'Child Thread'`? – Vinit Aug 02 '14 at 13:26
  • Execution of threads cannot be predicted. Hence there is no gurantee that created thread would finish before main thread. – Darshan Lila Aug 02 '14 at 13:29
  • Okay @Darshan, I got to know that as `'main'` thread is not a `daemon thread` in Java, its a non daemon or user thread. So its execution with respect to other competing threads can not be predicted. – Vinit Aug 02 '14 at 13:43
0

Daemon threads are the threads that are pulled automatically by the JVM as and when required. Garbage collector is one among. Main thread is pulled by user. So it ia non-daemon thread.

JVM shuts down only when all non-daemon threads complete execution. It does not wait for daemon threads to complete.

rithesh
  • 1
  • 1