一般服务端应用都是多线程的程序,它服务于多个请求,程序发生死锁后,死锁的线程处于等待状态(WAITING或TIMED_WAITING),等待状态的线程不占用cpu,消耗的内存也很有限,而表现上可能是请求没法进行,最后超时了。在死锁情况不多的时候,这种情况不容易被发现。

死锁可以使用jstack工具来查看

jps查看java进程ID

[root@appserver suninf]# jps -l
3986 sun.tools.jps.Jps
26873 /root/suninf/server/java/target/vps-utils-1.0.0.jar

jstack查看死锁问题

jstack的好处就是会把产生死锁的信息(包含是什么线程产生的)输出到最后,所以我们只需要看最后的内容就行了

Java stack information for the threads listed above:
===================================================
"Thread-4":
	at com.spareyaya.jvm.service.DeadLockService.service2(DeadLockService.java:35)
	- waiting to lock <0x00000000f5035ae0> (a java.lang.Object)
	- locked <0x00000000f5035af0> (a java.lang.Object)
	at com.spareyaya.jvm.controller.JVMController.lambda$deadLock$1(JVMController.java:41)
	at com.spareyaya.jvm.controller.JVMController$$Lambda$457/1776922136.run(Unknown Source)
	at java.lang.Thread.run(Thread.java:748)
"Thread-3":
	at com.spareyaya.jvm.service.DeadLockService.service1(DeadLockService.java:27)
	- waiting to lock <0x00000000f5035af0> (a java.lang.Object)
	- locked <0x00000000f5035ae0> (a java.lang.Object)
	at com.spareyaya.jvm.controller.JVMController.lambda$deadLock$0(JVMController.java:37)
	at com.spareyaya.jvm.controller.JVMController$$Lambda$456/474286897.run(Unknown Source)
	at java.lang.Thread.run(Thread.java:748)

Found 1 deadlock.