java 死锁检测工具_Java精通并发-死锁检测与相关工具详解

本文深入探讨Java中的死锁现象,通过实例演示了两个线程如何因互相等待对方持有的锁而导致死锁的情况,并介绍了如何使用jvisualvm等工具进行死锁检测。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

关于死锁其实在之前https://www.cnblogs.com/webor2006/p/10659938.html的jvm学习中已经详细举过例子了,不过这里再来复习一下,另外是从并发这个专题领域的角度再来看下它,这里先来阐述一下相关的概念:

死锁:线程1等待线程2互斥持有的资源,而线程2也在等待线程1互斥持有的资源,两个线程都无法继续执行。

活锁:线程持续重试一个总是失败的操作,导致无法继续执行。

饿死:线程一直被调度器延迟访问其赖以执行的资源,也许是调度器先于低优先级的线程而执行高优先级的线程,同时总是会有一个高优先级的线程可以执行,饿死也叫无限延迟。

以上是在Java并发中会遇到的三种情况,这次主要是来探讨死锁,先来编写一个死锁程序:

4bbf77a989efdfd412a4a97857ae61e7.png

然后再创建线程调用一下:

public classMyTest6 {private Object lock1 = newObject();private Object lock2 = newObject();public voidmyMethod1() {synchronized(lock1) {synchronized(lock2) {

System.out.println("myMethod1 invoked");

}

}

}public voidmyMethod2() {synchronized(lock2) {synchronized(lock1) {

System.out.println("myMethod2 invoked");

}

}

}public static void main(String[] args) {

MyTest6 myTest6 = new MyTest6();

Runnable runnable1 = () -> {

while (true) {

myTest6.myMethod1();

try {

Thread.sleep(100);

} catch (InterruptedException e) {

e.printStackTrace();

}

}

};

Thread thread1 = new Thread(runnable1, "myThread1");

Runnable runnable2 = () -> {

while (true) {

myTest6.myMethod2();

try {

Thread.sleep(250);

} catch (InterruptedException e) {

e.printStackTrace();

}

}

};

Thread thread2 = new Thread(runnable2, "myThread2");

thread1.start();

thread2.start();

}

}

然后运行一段时间后,死锁就会出现了:

c2930ea6e20a450a40aa270f505c5eb4.png

而到底是不是死锁了呢?可以采用jvisualvm工具可以进行死锁的检测,具体工具如何使用这里就不多说了,之前在JVM详细学习了,如下:

490d2dd89cd824513affb185aee43c55.png

此时我们可以点击“线程dump”来进行详细死锁的跟踪:

47bf36cc9da4d95f238b5cafc46e00a8.png

8a193415b343925ca110aae41519f619.png

其中看到的"BLOCKED"状态其实是在Thread类中的枚举值所定义的,如下:

public enumState {/*** Thread state for a thread which has not yet started.*/NEW,/*** Thread state for a runnable thread. A thread in the runnable

* state is executing in the Java virtual machine but it may

* be waiting for other resources from the operating system

* such as processor.*/RUNNABLE,/*** Thread state for a thread blocked waiting for a monitor lock.

* A thread in the blocked state is waiting for a monitor lock

* to enter a synchronized block/method or

* reenter a synchronized block/method after calling

* {@linkObject#wait() Object.wait}.*/BLOCKED,/*** Thread state for a waiting thread.

* A thread is in the waiting state due to calling one of the

* following methods:

*

*

{@linkObject#wait() Object.wait} with no timeout

*

{@link#join() Thread.join} with no timeout

*

{@linkLockSupport#park() LockSupport.park}

*

*

*

A thread in the waiting state is waiting for another thread to

* perform a particular action.

*

* For example, a thread that has called Object.wait()

* on an object is waiting for another thread to call

* Object.notify() or Object.notifyAll() on

* that object. A thread that has called Thread.join()

* is waiting for a specified thread to terminate.*/WAITING,/*** Thread state for a waiting thread with a specified waiting time.

* A thread is in the timed waiting state due to calling one of

* the following methods with a specified positive waiting time:

*

*

{@link#sleep Thread.sleep}

*

{@linkObject#wait(long) Object.wait} with timeout

*

{@link#join(long) Thread.join} with timeout

*

{@linkLockSupport#parkNanos LockSupport.parkNanos}

*

{@linkLockSupport#parkUntil LockSupport.parkUntil}

*

*/TIMED_WAITING,/*** Thread state for a terminated thread.

* The thread has completed execution.*/TERMINATED;

}

另外,在dump信息最后还有一个对死锁的总述:

83334704216a061d233c8f3757d2d369.png

这是用的可视化的工具来查看死锁的工具,但有时可能操作系统不支持查看可视化的工具,比如一些服务器只支持命令的方式来查看,此时就可以换另外一个JDK自带的工具来查看,如下:

59bd76b9adaf6755d2cee382b138f4bb.png

其中的输出信息跟在jvisualvm所看到的是一样的,只是是在命令行中来查看的:

9e6d65cfe00a9854b71102e2c8481530.png

另外还可以用强大的JMS工具来查看:

1e21051ed5ca7a064cb00b19aaee5cf2.png

efc8b9618c914161e95d9c909b95196c.png

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值