首页  

java死锁实例     所属分类 java 浏览量 993
两个锁 ,互相等待 ,进入死锁状态
jstack 会输出死锁信息
避免死锁
避免多个锁嵌套使用
多个锁按顺序加锁
使用支持超时的锁 ,譬如 ReentrantLock public boolean tryLock(long timeout, TimeUnit unit)



基于 synchronized  死锁例子
https://gitee.com/dyyx/hellocode/blob/master/src/DeadLock.java

jstack 输出的死锁信息

Found one Java-level deadlock:
=============================
"Thread-1":
  waiting to lock monitor 0x00007fee980264b8 (object 0x0000000795854e60, a java.lang.Class),
  which is held by "Thread-0"
"Thread-0":
  waiting to lock monitor 0x00007fee98028ea8 (object 0x0000000795856a80, a java.lang.Class),
  which is held by "Thread-1"

Java stack information for the threads listed above:
===================================================
"Thread-1":
	at DeadLock$ThreadA.hello(DeadLock.java:39)
	- waiting to lock <0x0000000795854e60> (a java.lang.Class for DeadLock$ThreadA)
	at DeadLock$ThreadB.hello(DeadLock.java:56)
	- locked <0x0000000795856a80> (a java.lang.Class for DeadLock$ThreadB)
	at DeadLock$ThreadB.run(DeadLock.java:49)
"Thread-0":
	at DeadLock$ThreadB.hello(DeadLock.java:53)
	- waiting to lock <0x0000000795856a80> (a java.lang.Class for DeadLock$ThreadB)
	at DeadLock$ThreadA.hello(DeadLock.java:42)
	- locked <0x0000000795854e60> (a java.lang.Class for DeadLock$ThreadA)
	at DeadLock$ThreadA.run(DeadLock.java:35)

Found 1 deadlock.





基于 ReentrantLock 死锁例子
https://gitee.com/dyyx/hellocode/blob/master/src/DeadLock2.java


Found one Java-level deadlock:
=============================
"Thread-1":
  waiting for ownable synchronizer 0x00000007955f35c0, (a java.util.concurrent.locks.ReentrantLock$NonfairSync),
  which is held by "Thread-0"
"Thread-0":
  waiting for ownable synchronizer 0x00000007955f35f0, (a java.util.concurrent.locks.ReentrantLock$NonfairSync),
  which is held by "Thread-1"

Java stack information for the threads listed above:
===================================================
"Thread-1":
	at sun.misc.Unsafe.park(Native Method)
	- parking to wait for  <0x00000007955f35c0> (a java.util.concurrent.locks.ReentrantLock$NonfairSync)
	at java.util.concurrent.locks.LockSupport.park(LockSupport.java:175)
	at java.util.concurrent.locks.AbstractQueuedSynchronizer.parkAndCheckInterrupt(AbstractQueuedSynchronizer.java:836)
	at java.util.concurrent.locks.AbstractQueuedSynchronizer.acquireQueued(AbstractQueuedSynchronizer.java:870)
	at java.util.concurrent.locks.AbstractQueuedSynchronizer.acquire(AbstractQueuedSynchronizer.java:1199)
	at java.util.concurrent.locks.ReentrantLock$NonfairSync.lock(ReentrantLock.java:209)
	at java.util.concurrent.locks.ReentrantLock.lock(ReentrantLock.java:285)
	at DeadLock2$ThreadA.hello(DeadLock2.java:41)
	at DeadLock2$ThreadB.hello(DeadLock2.java:78)
	at DeadLock2$ThreadB.run(DeadLock2.java:69)
"Thread-0":
	at sun.misc.Unsafe.park(Native Method)
	- parking to wait for  <0x00000007955f35f0> (a java.util.concurrent.locks.ReentrantLock$NonfairSync)
	at java.util.concurrent.locks.LockSupport.park(LockSupport.java:175)
	at java.util.concurrent.locks.AbstractQueuedSynchronizer.parkAndCheckInterrupt(AbstractQueuedSynchronizer.java:836)
	at java.util.concurrent.locks.AbstractQueuedSynchronizer.acquireQueued(AbstractQueuedSynchronizer.java:870)
	at java.util.concurrent.locks.AbstractQueuedSynchronizer.acquire(AbstractQueuedSynchronizer.java:1199)
	at java.util.concurrent.locks.ReentrantLock$NonfairSync.lock(ReentrantLock.java:209)
	at java.util.concurrent.locks.ReentrantLock.lock(ReentrantLock.java:285)
	at DeadLock2$ThreadB.hello(DeadLock2.java:73)
	at DeadLock2$ThreadA.hello(DeadLock2.java:59)
	at DeadLock2$ThreadA.run(DeadLock2.java:37)

Found 1 deadlock.



ReentrantLock  tryLock  支持超时时间 ,可避免死锁

public static void hello(){
    // locka.lock();
	// 可以 用超时锁 ,可 解决 死锁问题
	try {
        boolean result = locka.tryLock(300, TimeUnit.MILLISECONDS);
	    if(!result) {
		    throw new RuntimeException("tryLock result is false");
	    }
	}catch(Throwable e) {
			throw new RuntimeException("tryLock error,"+e);
	}

上一篇     下一篇
基于 wait 和 notifyAll 实现简单的阻塞队列

基于 ReentrantLock Condition await signalAll 实现简单的阻塞队列

Synchronized 和 AQS 实现要点

SpringBoot启动过程简介

Springboot2 Tomcat自定义配置

springboot2 FileEncodingApplicationListener简介