views:

83

answers:

5

I have a Java application which doesn't end. The main method finishes, but threads remain active and the application doesn't end. The thing is, there don't appear to be any monitor locks / waits, so I can't see why it's not ending. According to Eclipse, I am left with two non-Daemon threads. One is labelled [DestroyJavaVM] (looks hopeful!) and the other seems to be blocked in Unsafe.park(boolean, long). How / where should I start investigating this?

The abridged stacktrace of the second thread is:

   Unsafe.park(boolean, long)
at LockSupport.park(Object)
at AbstractQueuedSynchronizer$ConditionObject.await()
at LinkedBlockingQueue<E>.take()
at ThreadPoolExecutor.getTask()
at ThreadPoolExecutor$Worker.run()
at Thread.run() 
A: 

Thread dumps and debuggers would be my guess.

duffymo
A: 

Not sure how large the application is but I would check all the Threads you've created and ensure that their run methods are cleanly exited when the application is done executing. Somewhere, within a thread, you may have code along the lines of:

public void run() {
    while(true) { //"true" or some condition that never gets a chance to be false
        //do thread related work
    }
}
gmale
A: 

Unsafe.park, despite the scary-sounding name, is typically used by all kinds of blocking calls (especially those in the new(ish) java.util.concurrent package).

If you look a few frames further down the stack, you'll like see something like java.util.concurrent.LinkedBlockingQueue.take (i.e. some JDK library class) followed by something like com.example.myapp.MyClass.getNextJob (i.e. your class that's using the library class).

If I had to hazard a guess, I'd say that you're making some kind of call that blocks forever - and so when there's nothing further to return, this thread just sits there waiting for the "next" item. You could resolve this by setting some kind of "finished" flag, and then either interrupting the waiting thread or giving the blocking call a timeout, getting it to check the flag. Depending on your code either of these or an alternative could be feasible, but hopefully this is enough to get you started.

Edit: after seeing the stacktrace, finnw is right that you need to shutdown your executor service.

Andrzej Doyle
+1  A: 

You need to do one of two things to terminate your ExecutorService thread:

  1. Specify a ThreadFactory that creates daemon threads (the ThreadFactoryBuilder class from Guava will make this easier.)
  2. Call shutdown() on your ExecutorService as part of the application shutdown (e.g. at the end of your main method.)
finnw
+1: `shutdown()` is the canonical way to do this - daemon threads would prevent this problem but are quite possibly not what is desired and *might* lead to issues later.
Andrzej Doyle
A: 

Your task is blocked waiting for data from the queue. Take doesn't have a timeout associated with it.

Maintain a reference to your task thread when it is created. At shutdown, call the interrupt method on the thread. You may also need to modify the work processing loop that calls take to exit when InterruptedException is caught.

Jim Rush