class lock1 implements Runnable{
public void run(){
synchronized(Main.l1){
System.out.println("1:我获得了第一个锁");
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
synchronized(Main.l2){
System.out.println("1:我获得了第二个锁");
}
}
}
}
class lock2 implements Runnable{
public void run(){
synchronized(Main.l2){
System.out.println("2:我获得了第一个锁");
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
synchronized(Main.l1){
System.out.println("2:我获得了第二个锁");
}
}
}
}
public class Main {
public static String l1="lock1";
public static String l2="lock2";
public static void main (String[] args) {
Thread lo1=new Thread(new lock1());
Thread lo2=new Thread(new lock2());
lo1.start();
lo2.start();
}
}简单实现一个死锁Java
最新推荐文章于 2025-11-10 09:30:00 发布
335

被折叠的 条评论
为什么被折叠?



