thread reaches end but isn't removed

Posted by pstanton on Stack Overflow See other posts from Stack Overflow or by pstanton
Published on 2009-12-15T10:59:47Z Indexed on 2010/06/08 5:32 UTC
Read the original article Hit count: 414

Filed under:
|

I create a bunch of threads to do some processing:

new Thread("upd-" + id){
    @Override
    public void run(){
    	try{
    		doSomething();
    	}
    	catch (Throwable e){
    		LOG.error("error", e);
    	}
    	finally{
    		LOG.debug("thread death");
    	}
    }
}.start();

I know i should be using a threadPool but i need to understand the following problem before i change it:

I'm using eclipse's debugger and looking at the threads in the debug pane which lists active threads.

Many of them complete as you would expect, and are removed from the debug pane, however some seem to stay in the list of active threads even though the log shows the "thread death" entry for these.

When i attempt to debug these threads, they either do not pause for debugging or show an error dialog: "A timeout occurred while retrieving stack frames for thread: upd-...".

there is some synchronization going on within the doSomething() call but i'm fairly sure it's ok and since the "thread death" log is being called i'm assuming these threads aren't deadlocked in that method.

i don't do any Thread.join()s, however i do call a third party API but doubt they do either.

Can anyone think of another reason these threads are lingering?

Thanks.

EDIT:

I created this test to check the Garbage Collection theory:

Thread thread = new Thread("!!!!!!!!!!!!!!!!")
{
    @Override
    public void run()
    {
    	System.out.println("running");
    	ThreadUs.sleepQuiet(5000);
    	System.out.println("finished"); // <-- thread removed from list here
    }
};
thread.start();
ThreadUs.sleepQuiet(10000);
System.out.println(thread.isAlive()); // <-- thread already removed from list but hasn't been GC'd
ThreadUs.sleepQuiet(10000);

this proves that it is nothing to do with garbage collection as eclipse removes the thread from the thread list as soon as it completes and isn't waiting for the object to be de-referenced/GC'd.

© Stack Overflow or respective owner

Related posts about java

Related posts about multithreading