说白了,就是我要的资源被你锁了,你要的资源被我锁了,所以大家永远僵持谁都动不了
Ex:
下面为死锁的例子
public class DeadLock implements Runnable {
private boolean flag;
static String o1 = new String("str1"), o2 = new String("str2");
public void run() {
System.out.println(flag);
if (flag) {
//给对象01上锁
synchronized (o1) {
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
//给对象02上锁
synchronized (o2) {
System.out.println("AAA");
}
}
} else {
//
给对象02上锁
synchronized (o2) {
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
//给对象01上锁
synchronized (o1) {
System.out.println("BBB");
}
}
}
}
public static void main(String[] args) {
DeadLock aaa = new DeadLock();
DeadLock bbb = new DeadLock();
aaa.flag = true;
bbb.flag = false;
Thread thA = new Thread(aaa);//上锁的顺序
o1,o2
Thread thB = new Thread(bbb);//上锁的顺序
o2,o1
thA.start();
thB.start();
}
}
二、jconsole检查死锁问题
配置
l JRE
打开:java\jdk1.5.0\jre\lib\management目录,修改management.properties文件
无SSL加密:(去掉161行注释)
#For RMI monitoring without SSL use the following line
com.sun.management.jmxremote.ssl=false
无用户密码验证:(去掉203行注释)
# For RMI monitoring without any checking use the following line
com.sun.management.jmxremote.authenticate=false