打印Java程序的線程棧信息方式

打印Java程序的線程棧信息

jstack可以得知當前線程的運行情況

安裝jstack等命令集,jstack是開發版本jdk的一部分,不是開發版的有可能找不到

yum install -y  java-1.8.0-openjdk-devel

查看要打印堆棧的java進程ID

jps -l

打印堆棧

sudo -u admin jstack pid  > jstack.txt

特別要註意的是jstack需要使用與進程一致的用戶才能正確導出堆棧,否則會報錯如下

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

線程池異常堆棧的坑

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<>());  //擴展TraceThreadPoolExecutor
        for (int i = 0; i < 5; i++) {
            // executor.submit(new DivTask(100,i));
            //改進方式一:
            //Future re = executor.submit(new DivTask(100, i));
            //re.get();
            //改進方式二:
            executor.execute(new DivTask(100,i));
        }
        //100.0
        //25.0
        //33.0
        //50.0
        //其中100/0的異常結果沒打印
        //線程池很有可能"吃掉程序拋出的異常
        //改進方式一:
        //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)
        //。。。
        //改進方式二:
        //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
        //擴展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.*;
/**
 * 擴展TraceThreadPoolExecutor,讓它在調度任務前先保存一下提交任務線程的堆棧信息
 */
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;
            }
        };
    }
}

以上為個人經驗,希望能給大傢一個參考,也希望大傢多多支持WalkonNet。

推薦閱讀: