in tomcat,if a webapp did stop a none daemon thread,tomcat can not be shutdown by shutdown.sh
for example:
public class demo implements ServletContex
You can also write a tiny java Instrumentation agent to wrap the thread constructor (and stack dump there), instead of trying to mod the rt.jar classes.
If you have control of the class you can catch the stack trace when it is created:
public class Test extends TimerTask {
final StackTraceElement[] callerStack;
public Test () {
callerStack = Thread.currentThread().getStackTrace();
}
@Override
public void run() {
System.out.println("AAAA");
System.out.println("Creator: "+Arrays.asList(callerStack));
}
@Override
public String toString () {
return "Test created by "+Arrays.asList(callerStack);
}
}
Here is the list of approaches, sorted from quickest / most reliable to slowest / hardest:
If you have the source of the class, create an exception in the constructor (without actually throwing it). You can simply examine or print it when you need to know when the thread was created.
If you don't have the sources, the thread name can be a good hint who created it.
If the name hints to a generic service (like java.util.Timer
), then you can create a conditional breakpoint in your IDE in the constructor. The condition should be the thread name; the debugger will then stop when someone creates a thread with this name.
If you don't have too many threads, set a breakpoint in the constructors of Thread
.
If you have many threads, attach a debugger to the app and freeze it. Then examine the stack traces.
If everything else fails, get the source code for the Java runtime and add logging code in the classes you want to observe, compile a new rt.jar
and replace the original one with your version. Don't try this in production, please.
If money isn't an issue, you can use dynamic tracing tools like Compuware APM or, if you're on Linux or Solaris, you can try SystemTap and dtrace, respectively.
If you have an IDE like Eclipse or Intellij, you can set break points in the various Thread constructors. You can remotely attach to process using the debugger. The debugger will stop the process when the thread is being constructed. You can observe things like the thread name. You can also use break points with condition if you want to match on the specific thread name.
Setting break points. https://www.ibm.com/developerworks/library/os-ecbug/
Remote debugging https://dzone.com/articles/a-practical-guide-to-java-remote-debugging-in-the