java定位死锁的三种⽅法(jstack、Arthas和Jvisualvm)authentic
⽬录idealab
死锁
死锁发⽣的原因
死锁发⽣的条件
1:通过jstack定位死锁信息
1.2:查看死锁线程的pid
thought provoking2:通过Arthas⼯具定位死锁
3. 通过 Jvisualvm 定位死锁
死锁的预防
总结
死锁
死锁:是指两个或两个以上的进程在执⾏过程中,因争夺资源⽽造成的⼀种互相等待的现象,若⽆外⼒作⽤,它们都将⽆法推进下去。
死锁发⽣的原因
死锁的发⽣是由于资源竞争导致的,导致死锁的原因如下:
系统资源不⾜,如果系统资源充⾜,死锁出现的可能性就很低。
进程(线程)运⾏推进的顺序不合适。
资源分配不当等。
死锁发⽣的条件
死锁的发⽣的四个必要条件:
1. 互斥条件:⼀个资源每次只能被⼀个进程使⽤。
smosnow2. 占有且等待:⼀个进程因请求资源⽽阻塞时,对已获得的资源保持不放。
3. 不可强⾏占有:进程(线程)已获得的资源,在未使⽤完之前,不能强⾏剥夺。
4. 循环等待条件:若⼲进程(线程)之间形成⼀种头尾相接的循环等待资源关系。
这四个条件是死锁的必要条件,只要系统发⽣死锁,这些条件必然成⽴,⽽只要上述条件之⼀不满⾜,就不会发⽣死锁。
1:通过jstack定位死锁信息
1.1:编写死锁代码
Lock lock1 = new ReentrantLock();
Lock lock2 = new ReentrantLock();
ExecutorService exectuorService = wFixedThreadPool(2);
exectuorService.submit(() -> {
lock1.lock();
try{
Thread.sleep(1000);
}catch(Exception e){}
try{}
山东摄影培训finally{
lock1.unlock();
lock2.unlock();
}
});
exectuorService.submit(() -> {
lock2.lock();
try{
Thread.sleep(1000);怎么清理毛孔
}catch(Exception e){}
try{}
finally{
六级高频词汇下载
lock1.unlock();
lock2.unlock();
}
});
1.2:查看死锁线程的pid
jps查看死锁的线程pid
使⽤ jstack -l pid 查看死锁信息
通过打印信息我们可以找到发⽣死锁的代码是在哪个位置
"DestroyJavaVM" #13 prio=5 os_prio=31 tid=0x00007f9a1d8fe800 nid=0xd03 waiting on condition [0x0000000000000000]
java.lang.Thread.State: RUNNABLE
Locked ownable synchronizers:
- None
"pool-1-thread-2" #12 prio=5 os_prio=31 tid=0x00007f9a1d8fe000 nid=0xa703 waiting on condition [0x000070000ff8e000]
java.lang.Thread.State: WAITING (parking)
at sun.misc.Unsafe.park(Native Method)
- parking to wait for <0x0000000795768cd8> (a urrent.locks.ReentrantLock$NonfairSync)
at urrent.locks.LockSupport.park(LockSupport.java:175)
at urrent.locks.AbstractQueuedSynchronizer.parkAndCheckInterrupt(AbstractQueuedSynchronizer.java:836)
at urrent.locks.AbstractQueuedSynchronizer.acquireQueued(AbstractQueuedSynchronizer.java:870)
at urrent.locks.AbstractQueuedSynchronizer.acquire(AbstractQueuedSynchronizer.java:1199)
at urrent.locks.ReentrantLock$NonfairSync.lock(ReentrantLock.java:209)
at urrent.locks.ReentrantLock.lock(ReentrantLock.java:285)
util.SlideTimeUnit.lambda$main$1(SlideTimeUnit.java:63)
util.SlideTimeUnit$$Lambda$2/565760380.run(Unknown Source)
at urrent.Executors$RunnableAdapter.call(Executors.java:511)
at urrent.FutureTask.run(FutureTask.java:266)
at urrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
at urrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
at java.lang.Thread.run(Thread.java:748)
Locked ownable synchronizers:
- <0x0000000795768d08> (a urrent.locks.ReentrantLock$NonfairSync)
- <0x0000000795a9e4e0> (a urrent.ThreadPoolExecutor$Worker)
"pool-1-thread-1" #11 prio=5 os_prio=31 tid=0x00007f9a2082c800 nid=0xa803 waiting on condition [0x000070000fe8b000]
java.lang.Thread.State: WAITING (parking)
at sun.misc.Unsafe.park(Native Method)
-
parking to wait for <0x0000000795768d08> (a urrent.locks.ReentrantLock$NonfairSync)
at urrent.locks.LockSupport.park(LockSupport.java:175)
at urrent.locks.AbstractQueuedSynchronizer.parkAndCheckInterrupt(AbstractQueuedSynchronizer.java:836)
at urrent.locks.AbstractQueuedSynchronizer.acquireQueued(AbstractQueuedSynchronizer.java:870)
at urrent.locks.AbstractQueuedSynchronizer.acquire(AbstractQueuedSynchronizer.java:1199)
at urrent.locks.ReentrantLock$NonfairSync.lock(ReentrantLock.java:209)
at urrent.locks.ReentrantLock.lock(ReentrantLock.java:285)
util.SlideTimeUnit.lambda$main$0(SlideTimeUnit.java:49)
util.SlideTimeUnit$$Lambda$1/596512129.run(Unknown Source)
at urrent.Executors$RunnableAdapter.call(Executors.java:511)
at urrent.FutureTask.run(FutureTask.java:266)
at urrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
at urrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
at java.lang.Thread.run(Thread.java:748)
Locked ownable synchronizers:
- <0x0000000795768cd8> (a urrent.locks.ReentrantLock$NonfairSync)
- <0x0000000795a9ba28> (a urrent.ThreadPoolExecutor$Worker)
"Service Thread" #10 daemon prio=9 os_prio=31 tid=0x00007f9a2082c000 nid=0x4103 runnable [0x0000000000000000]
java.lang.Thread.State: RUNNABLE
Locked ownable synchronizers:
- None
"C1 CompilerThread3" #9 daemon prio=9 os_prio=31 tid=0x00007f9a1e021800 nid=0x3f03 waiting on condition [0x0000000000000000] java.lang.Thread.State: RUNNABLE
2:通过Arthas⼯具定位死锁
2.1: 下载好Arthas的jar,然后运⾏
有⼀个 thread -b 就可以查看到死锁信息
[arthas@4182]$ thread -b
"pool-1-thread-2" Id=12 WAITING on urrent.locks.ReentrantLock$NonfairSync@2cb8a9a3 owned by "pool-1-thread-1" Id=11
at sun.misc.Unsafe.park(Native Method)
梦溪笔谈翻译- waiting on urrent.locks.ReentrantLock$NonfairSync@2cb8a9a3
at urrent.locks.LockSupport.park(LockSupport.java:175)
kb是什么意思
at urrent.locks.AbstractQueuedSynchronizer.parkAndCheckInterrupt(AbstractQueuedSynchronizer.java:836)
at urrent.locks.AbstractQueuedSynchronizer.acquireQueued(AbstractQueuedSynchronizer.java:870)
at urrent.locks.AbstractQueuedSynchronizer.acquire(AbstractQueuedSynchronizer.java:1199)小学英语试卷
at urrent.locks.ReentrantLock$NonfairSync.lock(ReentrantLock.java:209)
at urrent.locks.ReentrantLock.lock(ReentrantLock.java:285)
util.SlideTimeUnit.lambda$main$1(SlideTimeUnit.java:63)
util.SlideTimeUnit$$Lambda$2/565760380.run(Unknown Source)
at urrent.Executors$RunnableAdapter.call(Executors.java:511)
at urrent.FutureTask.run(FutureTask.java:266)
at urrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
at urrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
at java.lang.Thread.run(Thread.java:748)
Number of locked synchronizers = 2
- urrent.ThreadPoolExecutor$Worker@6433a2
- urrent.locks.ReentrantLock$NonfairSync@3a855d13 <---- but blocks 1 other threads!
3. 通过 Jvisualvm 定位死锁
Jvisualvm 是⼀种⾃带的可视化⼯具,往往在在本地执⾏。
通过 Jvisualvm 命令打开软件,选中进程,进⼊线程视图,会给出死锁提⽰:
死锁的预防
尽量避免使⽤多个锁,并且只有需要时才持有锁。
如果使⽤多个锁,⼀定要设计好锁的获取顺序。
使⽤带有超时的⽅法,为程序带来更多的可控性,⽐如指定获取锁的时间最多为5秒,超时就放弃。
通过⼀些代码静态检查⼯具发现可能存在的死锁问题,⽐如FindBugs。
总结
到此这篇关于java定位死锁的⽂章就介绍到这了,更多相关java定位死锁内容请搜索以前的⽂章或继续浏览下⾯的相关⽂章希望⼤家以后多多⽀持!