dev@glassfish.java.net

IMPORTANT - Daemon Threads

From: Byron Nevins <byron.nevins_at_oracle.com>
Date: Tue, 12 Oct 2010 23:01:19 -0700
Here is the message to take away if you don't read the rest of the email:

If you create a Thread in GlassFish -- always make it a daemon thread -- unless you have a very good reason not to.  I.e. use the OPPOSITE of the default for Thread creation.

It's trivial to do:

Thread.setDaemon(true)

=============================================

You all probably know this already but I'll repeat anyways:  when the main() method in a java application (even GlassFish is but a java application!) returns -- the JVM exits.

Unless there are non-daemon threads left running.  In that case the JVM waits for the threads to die.  Now if the thread is like this one:

"Thread-22" prio=6 tid=0x070c9400 nid=0x1608 runnable [0x08daf000]
   java.lang.Thread.State: RUNNABLE
  .... boring stack trace removed

    at com.sun.messaging.jmq.admin.jmsspi.JMSAdminImpl$StreamGobbler.run(JMSAdminImpl.java:1507)

it never dies and the JVM will be waiting forever -- i.e. the Server is now permanently hung. 

Daemon threads will identify themselves as daemon like this:
"fileinstall-C:\glassfish3\glassfish\nodes\localhost\i1/autodeploy/bundles/" daemon prio=6 tid=0x06176400 nid=0x5a0 in ...

We take care of this sort of problem now with a System.exit() after stopping services.  This is not ideal though.  Please be very careful when creating long-running threads!
We ought to be able to exit the JVM cleanly without resporting to the sledge hammer of System.exit().

Do you have a non-daemon thread running that ought to be a daemon thread?  Here is how you can find out:

(0) Start DAS
(1) run this in a browser:
http://localhost:4848/__asadmin/stop-domain?force=false
(2) ZOMBIE!!!  Run jstack on the Zombie process to see all the threads that are preventing an orderly shutdown.

(The browser technique will go away within a week so try it right away if you think you may have a "bad" thread)


--
Byron Nevins  -  Oracle Corporation
Home: 650-359-1290
Cell: 650-784-4123
Sierra: 209-295-2188