How to print thread stack information of Java program

  • 2021-11-13 01:42:52
  • OfStack

Print thread stack information of Java program

jstack can know how the current thread is running

Install command sets such as jstack, jstack is part 1 of the development version of jdk, and may not be found if it is not the development version


yum install -y  java-1.8.0-openjdk-devel

View the java process ID to print the stack


jps -l

Print stack


sudo -u admin jstack pid  > jstack.txt

Note in particular that jstack requires users with Process 1 to export the stack correctly, otherwise an error will be reported as follows

Unable to open socket file: target process not responding or HotSpot VM not loaded

Pits in Thread Pool Exception Stack


import java.util.concurrent.*;
public class DivTask implements Runnable{
    int a,b;
    public DivTask(int a, int b) {
        this.a = a;
        this.b = b;
    }
    @Override
    public void run() {
        double re = a/b;
        System.out.println(re);
    }
    public static void main(String[] args) throws InterruptedException, ExecutionException {
//        ThreadPoolExecutor executor = new ThreadPoolExecutor(0, Integer.MAX_VALUE, 0L, TimeUnit.SECONDS
//                , new SynchronousQueue<>());
        TraceThreadPoolExecutor executor = new TraceThreadPoolExecutor(0, Integer.MAX_VALUE, 0L, TimeUnit.SECONDS
                , new SynchronousQueue<>());  // Expand TraceThreadPoolExecutor
        for (int i = 0; i < 5; i++) {
            // executor.submit(new DivTask(100,i));
            // Methods of improvement 1 : 
            //Future re = executor.submit(new DivTask(100, i));
            //re.get();
            // Methods of improvement 2 : 
            executor.execute(new DivTask(100,i));
        }
        //100.0
        //25.0
        //33.0
        //50.0
        // Among them 100/0 The exception result of is not printed 
        // Thread pools are likely " Eat the exception thrown by the program 
        // Methods of improvement 1 : 
        //Exception in thread "main" java.util.concurrent.ExecutionException: java.lang.ArithmeticException: / by zero
        //	at java.util.concurrent.FutureTask.report(FutureTask.java:122)
        //	at java.util.concurrent.FutureTask.get(FutureTask.java:192)
        // . . . 
        // Methods of improvement 2 : 
        //Exception in thread "pool-1-thread-1" java.lang.ArithmeticException: / by zero
        //	at com.Test.DivTask.run(DivTask.java:15)
        //	at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
        //	at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
        //	at java.lang.Thread.run(Thread.java:748)
        //100.0
        //33.0
        //25.0
        //50.0
        // Expand TraceThreadPoolExecutor
        //java.lang.Exception: Client stack trace
        //	at com.Test.TraceThreadPoolExecutor.clientTrace(TraceThreadPoolExecutor.java:20)
        //	at com.Test.TraceThreadPoolExecutor.execute(TraceThreadPoolExecutor.java:12)
        //	at com.Test.DivTask.main(DivTask.java:29)
        //Exception in thread "pool-1-thread-1" java.lang.ArithmeticException: / by zero
        //	at com.Test.DivTask.run(DivTask.java:15)
        //	at com.Test.TraceThreadPoolExecutor.lambda$wrap$0(TraceThreadPoolExecutor.java:25)
        //	at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
        //	at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
        //	at java.lang.Thread.run(Thread.java:748)
        //100.0
        //25.0
        //33.0
        //50.0
    }
}

import java.util.concurrent.*;
/**
 *  Expand TraceThreadPoolExecutor So that it can be saved before scheduling tasks 1 Stack information for submitting task threads under 
 */
public class TraceThreadPoolExecutor extends ThreadPoolExecutor {
    public TraceThreadPoolExecutor(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit, BlockingQueue<Runnable> workQueue) {
        super(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue);
    }
    @Override
    public void execute(Runnable task) {
        super.execute(wrap(task,clientTrace(),Thread.currentThread().getName()));
    }
    @Override
    public Future<?> submit(Runnable task) {
        return super.submit(wrap(task,clientTrace(),Thread.currentThread().getName()));
    }
    private Exception clientTrace(){
        return new Exception("Client stack trace");
    }
    private Runnable wrap(final Runnable task,final Exception clientTrace,String clientThreadName){
        return () -> {
            try {
                task.run();
            } catch (Exception e) {
                clientTrace.printStackTrace();
                throw e;
            }
        };
    }
}

Related articles: