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?