一个无聊的死锁

来源:互联网 发布:淘宝特卖女装连衣裙 编辑:程序博客网 时间:2024/05/22 13:44


public class DeadLock {
static String lock1="lock1";
static String lock2="lock2";

static class Thread1 extends Thread{


@Override
public void run() {
while (true) {
synchronized (lock1) {
System.out.println(Thread.currentThread().getName()
+ "lock1");
synchronized (lock2) {
System.out.println(Thread.currentThread().getName()
+ "lock2");
}
}
}
}

}
static class Thread2 extends Thread{

@Override
public void run() {
while (true) {
synchronized (lock2) {
System.out.println(Thread.currentThread().getName()
+ "lock2");
synchronized (lock1) {
System.out.println(Thread.currentThread().getName()
+ "lock1");
}
}
}
}

}


public static void main(String[] args) {
Thread1 thread1=new Thread1();
thread1.start();
Thread2 thread2=new Thread2();
thread2.start();
}

}
0 0
原创粉丝点击