public class MyDeadLock {
public static void main(String[] args) {
String a = "a";
String b = "b";
Resources resources1 = new Resources(a, b);
Resources resources2 = new Resources(b, a);
new Thread(resources1).start();
new Thread(resources2).start();
}
}
class Resources implements Runnable {
String a;
String b;
public Resources(String a, String b) {
this.a = a;
this.b = b;
}
@Override
public void run() {
synchronized (a) {
System.out.println(Thread.currentThread().getName());
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
System.out.println(e);
}
synchronized (b) {
System.out.println(Thread.currentThread().getName());
}
}
}
}
手写一个死锁
最新推荐文章于 2022-08-16 16:15:00 发布