1 死锁程序
package com.demo;
public class DeadLock implements Runnable{
private static final Object o1= new Object();
private static final String o2 = new String("ss");
private int flag = 0;
public DeadLock setFlag(int flag) {
this.flag = flag;
return this;
}
public static void main(String[] args) throws InterruptedException {
DeadLock deadLock1 = new DeadLock();
DeadLock deadLock2 = new DeadLock();
deadLock1.setFlag(0);
deadLock2.setFlag(1);
Thread thread1 = new Thread(deadLock1,"thread1");
Thread thread2 = new Thread(deadLock2,"thread2");
thread1.start();
thread2.start();
}
@Override
public void run() {
if (flag == 0) {
synchronized (o1) {
System.out.println("拿到1锁.。。");
synchronized (o2) {
System.out.println("申请2锁成功");
}
}
} else {
synchronized (o2) {
System.out.println("拿到2锁。。。。");
synchronized (o1) {
System.out.println("申请1锁成功");
}
}
}
}
}
jps打印进程号
jstack结果:
Full thread dump Java HotSpot(TM) 64-Bit Server VM (25.111-b14 mixed mode):
"DestroyJavaVM" #16 prio=5 os_prio=0 tid=0x00000000035cb800 nid=0x90d4 waiting on condition [0x0000000000000000]
java.lang.Thread.State: RUNNABLE
"thread2" #15 prio=5 os_prio=0 tid=0x0000000022752000 nid=0x32d0 waiting for monitor entry [0x000000002307f000]
java.lang.Thread.State: BLOCKED (on object monitor)
at com.demo.DeadLock.run(DeadLock.java:61)
- waiting to lock <0x0000000756d6a090> (a java.lang.Object)
- locked <0x0000000756d6a0a0> (a java.lang.String)
at java.lang.Thread.run(Thread.java:745)
"thread1" #14 prio=5 os_prio&#