1匠楚,利用top命令,查看占用CPU資源最高的進(jìn)程厂财,注意是進(jìn)程
如下:top
2芋簿,根據(jù)進(jìn)程號(hào)獲取占用資源最高的線程號(hào)
top -Hp pid
其中-H表示線程模式,-p表示pid監(jiān)控璃饱,pid為進(jìn)程號(hào)与斤。
3,將十進(jìn)制的線程號(hào)轉(zhuǎn)化為16進(jìn)制
利用printf命令將10進(jìn)制轉(zhuǎn)化為16進(jìn)制
printf %x 4816
結(jié)果:12d0
4荚恶,查找問題代碼位置
jstack 4806|grep 12d0
結(jié)果如下:
"test thread" #8 prio=5 os_prio=0 tid=0x00007f280819e000 nid=0x12d0 runnable [0x00007f280d9a4000]
也可以具體打印出jstack查找代碼位置,如下所示:
"test thread" #8 prio=5 os_prio=0 tid=0x00007f280819e000 nid=0x12d0 runnable
.............................................................................
at com.sparker.SimpleApp.lambda$main$0(SimpleApp.java:31)
? ? ? ? at com.sparker.SimpleApp$$Lambda$1/250421012.run(Unknown Source)
? ? ? ? at java.lang.Thread.run(Thread.java:748)
5撩穿,查看源碼,對(duì)應(yīng)System.out.println("running");部分即為占用cpu資源的地方。
? ? ? ? Thread thread = new Thread(() -> {
? ? ? ? ? ? while (true) {
? ? ? ? ? ? ? ? System.out.println("running");
? ? ? ? ? ? }
? ? ? ? });
? ? ? ? thread.setName("test thread");
thread.start();