日期:2014-05-20 浏览次数:20846 次
public static void main(String[] args) {
final String r1 = "resource1";
final String r2 = "resource2";
Thread t1 = new Thread(){
@Override
public void run() {
synchronized (r1) {
System.out.println("是"+Thread.currentThread().getName()+"拿到了"+r1);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
synchronized (r2) {
}
}
}
};
Thread t2 = new Thread(){
@Override
public void run() {
synchronized (r2) {
System.out.println("是"+Thread.currentThread().getName()+"拿到了"+r2);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
synchronized (r1) {
}
}
}
};
t1.start();
t2.start();
}