两个线程:
线程1
锁o1,
执行块中
try睡1s试图锁o2
线程2
锁o2,
执行块中锁o1,
object式:
public class Test {
static Object o1 = new Object();
static Object o2 = new Object();
public static void main(String[] args) {
new Thread(new Runnable() {
public void run() {
synchronized (o1) {
System.out.println("线程1锁o1");
try {
Thread.sleep(1000);//让当前线程睡眠,保证让另一线程得到o2,防止这个线程启动一下连续获得o1和o2两个对象的锁。
synchronized (o2) {
System.out.println("线程1锁o2");
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}).start();
new Thread(new Runnable() {
public void run() {
synchronized (o2) {
System.out.println("线程2锁o2");
synchronized (o1) {
System.out.println("线程2锁o1");
}
}
}
}).start();
}
}
参考代码: