打印Java程序的线程栈信息方式
作者:天已青色等烟雨来 时间:2021-11-02 19:00:28
打印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;
}
};
}
}
来源:https://blog.csdn.net/x356982611/article/details/96147628
标签:打印,Java,线程栈
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
IntelliJ IDEA 安装教程2019.09.23(最新版)
2023-08-24 23:01:44
![](https://img.aspxhome.com/file/2023/5/58175_0s.jpg)
Java中关于二叉树层序遍历深入了解
2023-07-26 07:06:09
![](https://img.aspxhome.com/file/2023/6/61076_0s.png)
java easyUI实现自定义网格视图实例代码
2022-05-16 23:52:54
![](https://img.aspxhome.com/file/2023/0/61920_0s.jpg)
Flutter之Timer实现短信验证码获取60s倒计时功能的代码
2023-07-20 20:00:30
![](https://img.aspxhome.com/file/2023/8/89528_0s.gif)
java IO流文件的读写具体实例
2023-08-21 04:44:34
![](https://img.aspxhome.com/file/2023/2/58072_0s.jpg)
教你怎么用Java数组和链表实现栈
2023-10-29 08:13:57
![](https://img.aspxhome.com/file/2023/0/58720_0s.png)
结合线程池实现apache kafka消费者组的误区及解决方法
2023-08-06 15:40:31
![](https://img.aspxhome.com/file/2023/8/58038_0s.png)
java金钱处理方法实例详解
2023-05-29 10:38:29
SpringBoot 利用thymeleaf自定义错误页面
2023-11-29 08:29:55
![](https://img.aspxhome.com/file/2023/4/60054_0s.png)
Hibernate中Session增删改查操作代码详解
2022-12-25 17:14:22
![](https://img.aspxhome.com/file/2023/6/62826_0s.jpg)
详解Java对象结构与对象锁的升级
2021-12-05 16:18:38
![](https://img.aspxhome.com/file/2023/8/64808_0s.png)
Java使用JDBC实现Oracle用户认证的方法详解
2022-10-06 08:59:36
MyBatis中映射文件的使用案例代码
2021-09-02 23:55:41
![](https://img.aspxhome.com/file/2023/0/60910_0s.png)
Java concurrency线程池之线程池原理(二)_动力节点Java学院整理
2023-11-28 23:43:18
java二叉树的几种遍历递归与非递归实现代码
2022-06-29 19:19:47
![](https://img.aspxhome.com/file/2023/7/64787_0s.jpg)
Java编程实现非对称加密的方法详解
2023-08-24 01:21:26
JAVA如何按字节截取字符串
2023-11-25 13:31:41
springboot+vue部署按照及运行方法
2023-07-15 06:16:09
Java Synchronized锁失败案例及解决方案
2023-10-25 12:33:47
![](https://img.aspxhome.com/file/2023/3/61863_0s.png)
Android中bindService基本使用方法概述
2023-08-05 19:18:56
![](https://img.aspxhome.com/file/2023/6/85446_0s.png)