jQuery in Action, 3rd edition
The moose likes Threads and Synchronization and the fly likes Main thread and child thread Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "Main thread and child thread" Watch "Main thread and child thread" New topic

Main thread and child thread

Inder Kumar Rathore
Ranch Hand

Joined: Apr 14, 2010
Posts: 41
hello everybody.....

I have created some child thread from main thread......

and my main method terminate first and my other child threads are still running, as I coded it so....

My question is that does main thread has been terminated or it is waiting for its child thread to terminate first.....

As I have read ever thread(user thread not daemon thread) are independent.....

If this is true, our main thread should be terminated before child thread,and as gc is a daemon thread of main thread so it should also b terminated....

then who will de-allocate memory of child threads unused objects........
I am confused......


Living is easy with your eyes closed.....
Prateek Parekh
Ranch Hand

Joined: Apr 17, 2010
Posts: 34
Are you using the start() or run() method of Thread?

If you can provide a sample of your code, it would be easier to figure what is amiss.
Istvan Kovacs
Ranch Hand

Joined: May 06, 2010
Posts: 100
The main thread may exit before any threads it has created. If at least one of those threads is not a daemon thread and is alive, the application will not exit, and the other threads (daemon or not) will continue to run.
The GC thread is not 'daemon thread of main thread'. It's just a daemon thread. It will exit - along with all other daemon threads - when there are no non-daemon threads alive. Until then, it will continue and collect garbage.
Inder Kumar Rathore
Ranch Hand

Joined: Apr 14, 2010
Posts: 41
Thanks for reply....
I agree. Here's the link: http://aspose.com/file-tools
subject: Main thread and child thread
It's not a secret anymore!