打印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,线程栈
0
投稿
猜你喜欢
Android自定义View模仿即刻点赞数字切换效果实例
2023-08-26 12:45:39
HttpServletRequest对象常用功能_动力节点Java学院整理
2022-01-05 10:37:08
Unity C#打包AssetBundle与场景详解
2022-02-19 14:04:13
OpenCV和C++实现图像的翻转(镜像)、平移、旋转、仿射与透视变换
2023-07-14 23:47:22
SpringCloud实现灰度发布的方法步骤
2023-03-17 05:18:37
解决SpringMVC、tomcat、Intellij idea、ajax中文乱码问题
2023-10-20 10:16:23
Spring实战之属性覆盖占位符配置器用法示例
2023-02-02 00:05:30
详解Spring boot/Spring 统一错误处理方案的使用
2023-11-24 12:56:07
formfile文件上传使用示例
2023-06-23 03:41:58
Aspectj框架实战案例详解
2022-12-30 02:28:24
Java实现仿淘宝滑动验证码研究代码详解
2022-12-28 00:51:11
Android编程之播放器MediaPlayer实现均衡器效果示例
2022-02-25 11:52:32
Android在Fragment中实现监听触摸事件
2023-08-22 05:24:34
Android聊天工具基于socket实现
2021-12-06 04:40:01
Android 使用Vibrator服务实现点击按钮带有震动效果
2023-06-15 01:57:16
将来会是Python、Java、Golang三足鼎立的局面吗
2023-11-22 09:18:31
C# 邮件发送和接收实现代码
2021-11-23 03:32:36
IntelliJ IDEA maven 构建简单springmvc项目(图文教程)
2021-09-13 05:40:40
Java对List进行排序的两种实现方法
2022-10-31 13:40:06
在SpringBoot中通过jasypt进行加密解密的方法
2023-11-15 21:29:23