[size=medium][color=brown][b]java的一个死锁的例子[/b][/color][/size]
package com.out.test;
public class Test6 implements Runnable{
public static Object o1 = new Object();
public static Object o2 = new Object();
public static int i = 1;
public int flag=1;
public void run() {
System.out.println(i++ + "线程启动。。。。");
if (flag == 1) {
synchronized(o1) {
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
synchronized (o2) {
System.out.println(i + "线程结束");
}
}
}
if (flag == 0) {
synchronized(o2) {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
synchronized (o1) {
System.out.println(i + "线程结束");
}
}
}
}
public static void main(String[] args) {
Test6 lock1 = new Test6();
Test6 lock2 = new Test6();
lock1.flag = 1;
lock2.flag = 0;
Thread thread1 = new Thread(lock1);
Thread thread2 = new Thread(lock2);
thread1.start();
thread2.start();
}
}