public class MyRunnable1 implements Runnable{
String lock1=”abc”;
String lock2=”dds”;
public void run() {
synchronized (lock1) {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("我在等lock2");
}
synchronized (lock2) {
}
}
public class MyRunnable2 implements Runnable{
String lock1=”abc”;
String lock2=”dds”;
public void run() {
synchronized (lock2) {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("我在等你把lock1给我");
}
synchronized (lock1) {
}
}
public class Test {
public static void main(String[] args) {
MyRunnable1 t1=new MyRunnable1();
MyRunnable2 t2=new MyRunnable2();
Thread run1=new Thread(t1,"张三");
Thread run2=new Thread(t2,"李四");
run1.start();
run2.start();
}