public class DeadLock {
private static Object lock1 = ``new
Object();
private static Object lock2 = ``new
Object();
public static void main(String[] args) {
new
Thread() {
@Override
public void run() {
synchronized (lock1) {
System.out.println(``"thread1 get lock1"``);
try
{
Thread.sleep(100);
} ``catch
(InterruptedException e) {
e.printStackTrace();
}
synchronized (lock2) {
System.out.println(``"thread1 get lock2"``);
}
}
System.out.println(``"thread1 end"``);
}
}.start();
new
Thread() {
@Override
public void run() {
synchronized (lock2) {
System.out.println(``"thread2 get lock2"``);
try
{
Thread.sleep(100);
} ``catch
(InterruptedException e) {
e.printStackTrace();
}
synchronized (lock1) {
System.out.println(``"thread2 get lock1"``);
}
}
System.out.println(``"thread2 end"``);
}
}.start();
}
}
欢迎来到这里!
我们正在构建一个小众社区,大家在这里相互信任,以平等 • 自由 • 奔放的价值观进行分享交流。最终,希望大家能够找到与自己志同道合的伙伴,共同成长。
注册 关于