您好,登录后才能下订单哦!
密码登录
登录注册
点击 登录注册 即表示同意《亿速云用户服务条款》
这篇文章主要讲解了“如何打印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; } }; } }
感谢各位的阅读,以上就是“如何打印Java程序的线程栈信息”的内容了,经过本文的学习后,相信大家对如何打印Java程序的线程栈信息这一问题有了更深刻的体会,具体使用情况还需要大家实践验证。这里是亿速云,小编将为大家推送更多相关知识点的文章,欢迎关注!
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。