本节介绍线程死锁,死锁的代码示例说明。怎么避免死锁,死锁的4个必要条件。
死锁是指多个线程竞争资源造成相互阻塞的现象
public class DeadLock{
private static Object resource1 = new Object();
private static Object resource2 = new Object();
public static void main(String[] args) {
new Thread(() -> {
synchronized (resource1) {
System.out.println(Thread.currentThread() + "得到资源1");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread() + "等待资源2");
synchronized (resource2) {
System.out.println(Thread.currentThread() + "得到资源2");
}
}
}, "线程 1").start();
new Thread(() -> {
synchronized (resource2) {
System.out.println(Thread.currentThread() + "得到资源2");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread() + "等待资源1");
synchronized (resource1) {
System.out.println(Thread.currentThread() + "得到资源1");
}
}
}, "线程 2").start();
}
}
运行结果