Hello there,
I've developed an application using the Classworlds framework, and I'd
like to discuss the Thread behavior inside it.
Since release 1.1, the 'System.exit' invocation inside Launcher.main
method is ending any Thread started in the target class main method.
Dan Diephouse suggested me a shutdown hook as shown here:
http://svn.plexus.codehaus.org/trunk/plexus-containers/plexus-container-default/src/main/java/org/codehaus/plexus/PlexusContainerHost.java?rev=1750&view=auto
Unfortunately, this solution didn't work for us.
We're using junit.swingui.TestRunner main method inside our class, and
its Swing window gets closed when the addShutdownHook is invoked, even
though the thread is still alive.
I would like to propose you a solution, that won't remove the original
'exit code', but will include a Thread support.
It's a simple loop that checks for the existence of any active threads
other than the main thread (please see code below):
---------------------------------------------------------
public static
void main( String[] args )
{
try
{
int initialActiveCount =
Thread.activeCount();
int exitCode = mainWithExitCode( args );
// wait for main target Threads
while ( Thread.activeCount() > initialActiveCount )
{
try
{
Thread.sleep( 100 );
}
catch ( InterruptedException ignored )
{
// ignore
}
}
System.exit( exitCode );
}
catch ( Exception e )
{
e.printStackTrace();
System.exit( 100 );
}
}
-----------------------------------------------
Thanks!
Best Regards,
Fernando Lindner