In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
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!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.