首页 > 应用开发 > Java > 什么是死锁?如何解决死锁?
public class DeadLock {
public static final String LOCK_1 = "lock1";
public static final String LOCK_2 = "lock2";
public static void main(String[] args) {
Thread threadA = new Thread(() -> {
try {
while (true) {
synchronized (DeadLock.LOCK_1) {
System.out.println(Thread.currentThread().getName() + " 锁住 lock1");
Thread.sleep(1000);
synchronized (DeadLock.LOCK_2) {
System.out.println(Thread.currentThread().getName() + " 锁住 lock2");
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
});
Thread threadB = new Thread(() -> {
try {
while (true) {
synchronized (DeadLock.LOCK_2) {
System.out.println(Thread.currentThread().getName() + " 锁住 lock2");
Thread.sleep(1000);
synchronized (DeadLock.LOCK_1) {
System.out.println(Thread.currentThread().getName() + " 锁住 lock1");
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
});
threadA.start();
threadB.start();
}
}
public class DeadLock {
public static final String LOCK_1 = "lock1";
public static final String LOCK_2 = "lock2";
public static void main(String[] args) {
Thread threadA = new Thread(() -> {
try {
while (true) {
synchronized (DeadLock.LOCK_1) {
System.out.println(Thread.currentThread().getName() + " 锁住 lock1");
Thread.sleep(1000);
synchronized (DeadLock.LOCK_2) {
System.out.println(Thread.currentThread().getName() + " 锁住 lock2");
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
});
Thread threadB = new Thread(() -> {
try {
while (true) {
synchronized (DeadLock.LOCK_1) {
System.out.println(Thread.currentThread().getName() + " 锁住 lock1");
Thread.sleep(1000);
synchronized (DeadLock.LOCK_2) {
System.out.println(Thread.currentThread().getName() + " 锁住 lock2");
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
});
threadA.start();
threadB.start();
}
}
public static final String LOCK_1 = "lock"; public static final String LOCK_2 = "lock";
来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/69976011/viewspace-2710964/ ,如需转载,请注明出处,否则将追究法律责任。