public class Demo{
public static void main(String[] args) {
Object o1=new Object();
Object o2=new Object();
new Thread(()->{
synchronized (o2) {
for (int i = 0; i < 5; i++) {
System.out.println("Thread1 running");
ThreadUtil.sleep(1000);
}
synchronized (o1) {
System.out.println("hello world");
}
}
}).start();
new Thread(()->{
synchronized (o1) {
for (int i = 0; i < 5; i++) {
System.out.println("Thread2 running");
ThreadUtil.sleep(1000);
}
synchronized (o2) {
System.out.println("hello world");
}
}
}).start();
}
}
java死锁代码
最新推荐文章于 2022-06-19 17:36:16 发布