package com.cfd.thread;
/**
* 死锁
* @author cfd
*
*/
class DLockThread extends Thread{
static Object a = new Object();
static Object b = new Object();
String firstObj = "";
public DLockThread(String firstObj, String threadName){
this.firstObj = firstObj;
super.setName(threadName);
}
public void run() {
while(true)
if("a".equals(firstObj)){
synchronized(a){//获取对象a的锁,然后去获取对象b的锁
System.out.println(Thread.currentThread().getName()+"--->"+a.toString());
synchronized(b){
System.out.println(Thread.currentThread().getName()+"--->"+b.toString());
}
}
}else if("b".equals(firstObj)) {
synchronized(b){
System.out.println(Thread.currentThread().getName()+"--->"+b.toString());
synchronized(a){
System.out.println(Thread.currentThread().getName()+"--->"+a.toString());
}
}
}
}
}
public class DLockThreadTest{
public static void main(String[] args) {
DLockThread t1 = new DLockThread("a", "t1");
DLockThread t2 = new DLockThread("b", "t2");
t1.start();
t2.start();
}
}