死锁的本质,举个例子如果此时有一个线程A ,按照先获持有锁a再获取锁b的顺序获得 锁,同时另外一个线程B,按照先获取锁b再获取锁a的顺序获取锁
通过jdk工具jps、jstack排查死锁问题
步骤一:使用jsp查找程序进行
gujiachundeMacBook-Pro:~ gujiachun$ jps
660 nacos-server.jar
83479
96521 DeadLock
96572 Jps
步骤二:使用jstack查看线程堆栈信息
jstack: jdk提供的一个工具,可以查看java进程中线程堆栈信息。更详细的用法见文档最 后。
$ jstack 96521
Found one Java-level deadlock:
■Thread-1":
waiting to lock monitor 0x00007f9a2802d418 (object 0x000000076ab92080, a java.lang.Object), which is held by ■Thread-O"
-Thread-0":
waiting to lock monitor 0x00007f9a2802f9e8 (object 0x000000076ab92090, a java.lang.Object), which is held by "Thread-1"
Java stack information for the threads listed above:
"Thread-1/:
at com.rainbow.demo.service.DeadLock.lambda$l(DeadLock.java:32)
-waiting to lock <0x000000076ab92080> (a java.lang.Object)
-locked <0x000000076ab92090> (a java.lang.Object)
at com.rainbow.demo.service.DeadLock$Lambda2/1044036744.run(Unknown Source)
at java.lang.Thread.run(Thread.java:748)
"Thread-0":
at com.rainbow.demo.service.DeadLock.lambdaS0(DeadLock.java:18)
-waiting to lock <0x000000076ab92090> (a java.lang.Object)
-locked <0x000000076ab92080> (a java.lang.Object)
at com.rainbow.demo.service.DeadLock$Lambdal/303563356.run(Unknown Source)
at java.lang.Thread.run(Thread.java:748)
Found 1 deadlock.
死锁的代码是在DeadLock.java的32行和18行,此时我们就可以去优化代码,解决死锁问题。

Was this helpful?

0 / 0

发表回复 0

Your email address will not be published.