//
/* * 死锁是指两个或者以上的进程在执行过程中,
- 因争夺资源而造成的一种相互等待的现象,
- 若无外力干涉那他们都将无法推进下去
定位:jps -l 找到死锁的进程号
jstack 进程号
**/
class DeadLock implements Runnable{
private String key1;
private String key2;
public DeadLock(String key1, String key2) {
this.key1 = key1;
this.key2 = key2;
}
@Override
public void run() {
synchronized (key1){
System.out.println(Thread.currentThread().getName()+“拥有锁”+key1+“尝试获取锁”+key2);
synchronized (key2){
System.out.println(Thread.currentThread().getName()+“拥有锁”+key2+“尝试获取锁”+key1);
}
}
}
}
public class DeadLockDemo {
public static void main(String[] args) {
String key1=“key1”;
String key2=“key2”;
new Thread(new DeadLock(key1,key2),“aa”).start();
new Thread(new DeadLock(key2,key1),“bb”).start();
}
}
死锁的运行结果:
死锁的定位以及分析:jps -l
找到死锁的进程号:21044