您好,登錄后才能下訂單哦!
這篇文章主要講解了“如何打印Java程序的線程棧信息”,文中的講解內(nèi)容簡(jiǎn)單清晰,易于學(xué)習(xí)與理解,下面請(qǐng)大家跟著小編的思路慢慢深入,一起來研究和學(xué)習(xí)“如何打印Java程序的線程棧信息”吧!
jstack可以得知當(dāng)前線程的運(yùn)行情況
安裝jstack等命令集,jstack是開發(fā)版本jdk的一部分,不是開發(fā)版的有可能找不到
yum install -y java-1.8.0-openjdk-devel
查看要打印堆棧的java進(jìn)程ID
jps -l
sudo -u admin jstack pid > jstack.txt
特別要注意的是jstack需要使用與進(jìn)程一致的用戶才能正確導(dǎo)出堆棧,否則會(huì)報(bào)錯(cuò)如下
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<>()); //擴(kuò)展TraceThreadPoolExecutor for (int i = 0; i < 5; i++) { // executor.submit(new DivTask(100,i)); //改進(jìn)方式一: //Future re = executor.submit(new DivTask(100, i)); //re.get(); //改進(jìn)方式二: executor.execute(new DivTask(100,i)); } //100.0 //25.0 //33.0 //50.0 //其中100/0的異常結(jié)果沒打印 //線程池很有可能"吃掉程序拋出的異常 //改進(jìn)方式一: //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) //。。。 //改進(jìn)方式二: //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 //擴(kuò)展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.*; /** * 擴(kuò)展TraceThreadPoolExecutor,讓它在調(diào)度任務(wù)前先保存一下提交任務(wù)線程的堆棧信息 */ 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程序的線程棧信息”的內(nèi)容了,經(jīng)過本文的學(xué)習(xí)后,相信大家對(duì)如何打印Java程序的線程棧信息這一問題有了更深刻的體會(huì),具體使用情況還需要大家實(shí)踐驗(yàn)證。這里是億速云,小編將為大家推送更多相關(guān)知識(shí)點(diǎn)的文章,歡迎關(guān)注!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。