Get the App
SLTechnology News&Howtos  ›  Development  › 

How to print thread stack information of Java program

Shulou Source: shulou.com Published: 2022-06-03 15:51:23 11月14日 Update

This article mainly explains "how to print thread stack information of Java program". The content of the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "how to print thread stack information of Java program".

Print thread stack information for Java programs

Jstack can know how the current thread is running.

Install command sets such as jstack. Jstack is part 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 where you want to print the stack

Jps-l print stack sudo-u admin jstack pid > jstack.txt

It is important to note that jstack needs to use a user consistent with the process 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

Import java.util.concurrent.*;public class DivTask implements Runnable {int ajar b; public DivTask (int a, int b) {this.a = a; this.b = b;} @ Override public void run () {double re = a gamb; 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 ()); / / extended 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 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;}} }} Thank you for your reading, the above is the content of "how to print thread stack information of Java program". After the study of this article, I believe you have a deeper understanding of how to print thread stack information of Java program, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Tags: Threads information programs stacks ways learning tasks content situations processes development consistency commands that is ideas articles more versions users knowledge Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Apple MariaDB Shulou Technology vpn Shulou Tech Info