文章目录
死锁
java
public class MyThread extends Thread {
//创建锁对象
static Object objA = new Object();
static Object objB = new Object();
@Override
public void run() {
while (true) {
if (getName().equals("线程1")) {
synchronized (objA) {
System.out.println("线程1获得A锁,准备B锁");
synchronized (objB) {
System.out.println("线程1获得A锁与B锁");
}
}
} else if (getName().equals("线程2")) {
synchronized (objB) {
System.out.println("线程2获得B锁,准备A锁");
synchronized (objA) {
System.out.println("线程2获得B锁与A锁");
}
}
}
}
}
}
---------------------------
线程1获得A锁,准备B锁
线程2获得B锁,准备A锁