2.2线程运行诊断
案例1:cpu占用过多
定位
用top定位哪个进程对cpu的占用过高
ps H -eo pid,tid,%cpu |grep 进程id(用ps命令进一步定位是哪个线程引起的cpu占用过高)
jstack 进程id
可以根据线程id找到有问题的线程,进一步定位到有问题代码的源码
注意:ps查到的线程编号是十进制的,jstack显示出的线程编号是十六进制的,故在使用jstack时,请先将十进制线程编号转化为十六进制
案例2:程序运行很长时间没有结果
如下代码会发生死锁,请使用jstack命令自行排查问题。
class A{};
class B{};
public class Demo{
static A a=new A();
static B b=new B();
public static void main(String[] args) throws InterruptedException{
new Thread(()->{
synchronized(a){
try{
Thread.sleep(2000);
}catch(InterruptedException e){
e.printStackTrace();
}
synchronized(b){
System.out.println("我获得了a和b");
}
}
}).start();
Thread.sleep(1000);
new Thread(()->{
synchronized(b){
synchronized(a){
System.out.println("我获得了a和b");
}
}
}).start();
}
}
Last updated
Was this helpful?