threads: to join or not to join

This is a discussion on threads: to join or not to join within the Java forums in Programming Languages category; I have just discovered that when a java program starts one of more threads and then the main thread comes to the end of function main, the program will wait for the other threads to complete unless the threads are daemon threads. This comes as quite a suprise to me. Does this mean there is an implicit join? Regards, Andrew Marlow...

Go Back   Application Development Forum > Programming Languages > Java

Object Mix

Register FAQ Calendar Search Today's Posts Mark Forums Read
  #1  
Old 08-27-2008, 03:04 AM
marlow.andrew@googlemail.com
Guest
 
Default threads: to join or not to join

I have just discovered that when a java program starts one of more
threads and then the main thread comes to the end of function main,
the program will wait for the other threads to complete unless the
threads are daemon threads. This comes as quite a suprise to me. Does
this mean there is an implicit join?

Regards,

Andrew Marlow
Reply With Quote
  #2  
Old 08-27-2008, 07:06 AM
Claudio Nieder
Guest
 
Default Re: threads: to join or not to join

On Wed, 27 Aug 2008 00:04:45 -0700, marlow.andrew wrote:

> Does this mean there is an implicit join?


Sort of. If I look at the Sun JVM it's not an implcit join in your main
method, but it looks like at end of main DestroyVM is called which itself
waits for any non-daemon threads to terminate before it will bring down
the VM. You can see that the program below produces different outputs
depending on wether you perform a t.join() or not at the end of main.

import java.util.Arrays;
import java.util.Map;

public class D
{
public static void main(final String[] args) throws InterruptedException
{
Thread.currentThread().setName("Main thread");
ThreadGroup tg=Thread.currentThread().getThreadGroup();
while (tg.getParent()!=null) {
tg=tg.getParent();
}
final ThreadGroup top=tg;
final Thread t=new Thread(new Runnable() {
public void run()
{
for (int rounds=0; rounds<3; rounds++) {
System.out.println("--------------------");
System.out.flush();
final Map<Thread,StackTraceElement[]> stacks=Thread.getAllStackTraces
();
for (final Map.Entry<Thread,StackTraceElement[]>
entry:stacks.entrySet()) {
System.out.println("\nThread: "+entry.getKey().getName()+"\n "
+Arrays.toString(entry.getValue()).replaceAll(",", "\n"));
}
try {
Thread.sleep(1000);
} catch (final InterruptedException e) {
e.printStackTrace();
System.exit(1);
}
}
}
});
t.setName("MY Thread");
t.start();
// t.join();
System.out.println("End of main");
System.out.flush();
}
}

--
Claudio Nieder, Talweg 6, CH-8610 Uster, Tel +4179 357 6743,
www.claudio.ch
Reply With Quote
  #3  
Old 08-27-2008, 04:45 PM
marlow.andrew@googlemail.com
Guest
 
Default Re: threads: to join or not to join

On Aug 27, 12:06 pm, Claudio Nieder <priv...@claudio.ch> wrote:
> On Wed, 27 Aug 2008 00:04:45 -0700, marlow.andrew wrote:
> > Does this mean there is an implicit join?

>
> Sort of. If I look at the Sun JVM it's not an implcit join in your main
> method, but it looks like at end of main DestroyVM is called which itself
> waits for any non-daemon threads to terminate before it will bring down
> the VM.


The reason I ask is that I am preparing for the sun java exam and some
of the threading questions seem to require/assume this behaviour.

> You can see that the program below produces different outputs
> depending on wether you perform a t.join() or not at the end of main.

[ program snipped ]

er, I don't quite follow. If the cleanup at the end waits for any non-
daemon threads to finish then surely it should not make any difference
if you don't do t.join()?

Regards,

Andrew Marlow
Reply With Quote
  #4  
Old 08-27-2008, 11:29 PM
John B. Matthews
Guest
 
Default Re: threads: to join or not to join

In article
<429ae0eb-fad3-4835-8e04-570ea146bc77@f63g2000hsf.googlegroups.com>,
marlow.andrew@googlemail.com wrote:

> On Aug 27, 12:06 pm, Claudio Nieder <priv...@claudio.ch> wrote:
> > On Wed, 27 Aug 2008 00:04:45 -0700, marlow.andrew wrote:
> > > Does this mean there is an implicit join?

> >
> > Sort of. If I look at the Sun JVM it's not an implcit join in your main
> > method, but it looks like at end of main DestroyVM is called which itself
> > waits for any non-daemon threads to terminate before it will bring down
> > the VM.

>
> The reason I ask is that I am preparing for the sun java exam and some
> of the threading questions seem to require/assume this behaviour.
>
> > You can see that the program below produces different outputs
> > depending on wether you perform a t.join() or not at the end of main.

> [ program snipped ]
>
> er, I don't quite follow. If the cleanup at the end waits for any non-
> daemon threads to finish then surely it should not make any difference
> if you don't do t.join()?


Abbreviating the output:

With t.join(), main ends last:
--------------------
Round: 0
Thread: Main thread
Thread: Signal Dispatcher
Thread: My Thread
Thread: Finalizer
Thread: Reference Handler
--------------------
Round: 1
Thread: Main thread
Thread: Signal Dispatcher
Thread: My Thread
Thread: Finalizer
Thread: Reference Handler
* End of main *

Without t.join(), main ends almost immediately, and DestroyJavaVM
appears to collect the bodies:
* End of main *
--------------------
Round: 0
Thread: Signal Dispatcher
Thread: My Thread
Thread: Finalizer
Thread: Reference Handler
--------------------
Round: 1
Thread: Signal Dispatcher
Thread: My Thread
Thread: DestroyJavaVM
Thread: Finalizer
Thread: Reference Handler

--
John B. Matthews
trashgod at gmail dot com
home dot woh dot rr dot com slash jbmatthews
Reply With Quote
  #5  
Old 08-28-2008, 07:57 AM
Claudio Nieder
Guest
 
Default Re: threads: to join or not to join

Hi,

> The reason I ask is that I am preparing for the sun java exam and some
> of the threading questions seem to require/assume this behaviour.


It is specified in http://java.sun.com/docs/books/jvms/second_edition/
html/Concepts.doc.html#19152

> er, I don't quite follow. If the cleanup at the end waits for any non-
> daemon threads to finish then surely it should not make any difference
> if you don't do t.join()?


No. It would only make a difference if you want to do any action after the
thread terminates, as does my System.out.println("End of main");

claudio
--
Claudio Nieder, Talweg 6, CH-8610 Uster, Tel +4179 357 6743,
www.claudio.ch






--
Claudio Nieder, Talweg 6, CH-8610 Uster, Tel +4179 357 6743,
www.claudio.ch
Reply With Quote
Reply


Thread Tools
Display Modes


All times are GMT -5. The time now is 01:14 PM.


Powered by vBulletin® Version 3.7.2
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.2.0
vB Ad Management by =RedTyger=

In an effort to better serve ads to our visitors, cookies are used on objectmix.com. For more information, check out our Privacy Policy.