public class TestThread implements Runnable {
public int flag = 1;
static Object o1 = new Object();
static Object o2 = new Object();
@Override
public void run() {
System.out.println("flag" + flag);
if(flag == 1) {
synchronized(o1) {
try {
Thread.sleep(1000);
} catch(InterruptedException e) {
e.printStackTrace();
}
synchronized(o2) {
System.out.println("1");
}
}
}
if(flag == 0) {
synchronized(o2) {
try {
Thread.sleep(1000);
} catch(InterruptedException e) {
e.printStackTrace();
}
synchronized(o1) {
System.out.println("0");
}
}
}
}
public static void main(String[] args) {
TestThread td1 = new TestThread();
TestThread td2 = new TestThread();
td1.flag = 1;
td2.flag = 0;
Thread t1 = new Thread(td1);
Thread t2 = new Thread(td2);
t1.start();
t2.start();
}
}
[color=red][size=medium]线程t1运行的时候他锁定了o1,然后等待sleep1000以后准备锁定o2。可是在他sleep的时候t2已经锁定了o2,同时t2也等待o1.这样就出现了死锁。他们谁也不会得到另一个Object。[/size][/color]
面试题的一个例子:
public class TestThread implements Runnable {
int b = 100;
public synchronized void m1() throws Exception {
Thread.sleep(5000);
b = 1000;
System.out.println("b = " + b);
}
public void m2() {
System.out.println(b);
}
@Override
public void run() {
try {
m1();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void main(String[] args) throws Exception {
TestThread t1 = new TestThread();
Thread t = new Thread(t1);
t.start();
Thread.sleep(1000);
t1.m2();
}
}
运行结果:
100
b = 1000
[color=red][size=medium]从运行结果来看,m2方法被调用了。说明m1被锁定了,但是线程可以访问那些没有被锁定的方法。如果从m2改变b的值也是可以的。[/size][/color]