1. 程式人生 > >java死鎖示例及分析排查

java死鎖示例及分析排查

       死鎖是指多個執行緒迴圈等待它方佔有的資源而無限期地僵持下去的局面。如有o1與o2兩個資源,執行緒t1持有o1後,如果再持有o2,就能正常完成操作,不巧,這時另一個執行緒t2已經持有o2,若再持有o1,則能完成操作,這樣就互相等對方持有的資源,產生死鎖。

一 、 示例。

 1、定義資源

package thread.deadlock;


public class CustomObject {

}

2、執行緒1實現

package thread.deadlock;

public class LockT1 implements Runnable{
	private CustomObject o1;
	private CustomObject o2;
	
	
	public LockT1(CustomObject o1, CustomObject o2) {
		super();
		this.o1 = o1;
		this.o2 = o2;
	}


	@Override
	public void run() {
		// TODO Auto-generated method stub
		synchronized(o1){
			try {
				System.out.println(Thread.currentThread().getName()+ " hold o1");
				Thread.sleep(5000);
				synchronized(o2){
					System.out.println(Thread.currentThread().getName()+ " hold o2");
				}
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
	
}
2、執行緒2實現

package thread.deadlock;

public class LockT2 implements Runnable{
	private CustomObject o1;
	private CustomObject o2;
	
	
	public LockT2(CustomObject o1, CustomObject o2) {
		super();
		this.o1 = o1;
		this.o2 = o2;
	}


	@Override
	public void run() {
		// TODO Auto-generated method stub
		synchronized(o2){
			try {
				System.out.println(Thread.currentThread().getName()+ " hold o2");
				Thread.sleep(1000);
				synchronized(o1){
					System.out.println(Thread.currentThread().getName()+ " hold o1");
				}
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
	
}

3、跑起來
package thread.deadlock;

public class T {
	public static void main(String[] args) {
		CustomObject o1 = new CustomObject();
		CustomObject o2 = new CustomObject();
		Thread t1 = new Thread(new LockT1(o1,o2),"t1");
		Thread t2 = new Thread(new LockT2(o1,o2),"t2");
		t1.start();
		t2.start();
	}
}

二、排查

工具:jstack 或 jvisualvm.exe

dump執行緒執行資訊

Found one Java-level deadlock:
=============================
"t2":
  waiting to lock monitor 0x000000000226b168 (object 0x00000007ab11de78, a thread.deadlock.CustomObject),
  which is held by "t1"
"t1":
  waiting to lock monitor 0x0000000010bbe3e8 (object 0x00000007ab11de88, a thread.deadlock.CustomObject),
  which is held by "t2"

可以看到t1和t2執行緒相互持有待的資源,thread.deadlock.CustomObject 為鎖住的資源所屬的類