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.take()
at ThreadPoolExecutor.getTask()
at ThreadPoolExecutor$Worker.run()
at Thread.run()
解决方案
You need to do one of two things to terminate your ExecutorService thread:
Specify a ThreadFactory that creates daemon threads (the ThreadFactoryBuilder class from Guava will make this easier.)
Call shutdown() on your ExecutorService as part of the application shutdown (e.g. at the end of your main method.)