how to find out who create a thread in java?

后端 未结 4 1342
鱼传尺愫
鱼传尺愫 2020-12-29 15:13

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         


        
4条回答
  •  攒了一身酷
    2020-12-29 15:30

    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);
      }
    }
    

提交回复
热议问题