死锁代码及其验证
代码
public class DeadLock {
public static void main(String[] args) {
Object lock1 = new Object();
Object lock2 = new Object();
Thread t1 = new Thread(new Runnable() {
@Override
public void run() {
synchronized (lock1) {
System.out.print(Thread.currentThread().getName());
try {
Thread.sleep(1000);
} catch (Exception e) {
e.printStackTrace();
}
synchronized (lock2) {
System.out.println();
}
}
}
}, "ASC");
Thread t2 = new Thread(new Runnable() {
public void run() {
synchronized (lock2) {
System.out.print(Thread.currentThread().getName());
try {
Thread.sleep(1000);
} catch (Exception e) {
e.printStackTrace();
}
synchronized (lock1) {
System.out.println();
}
}
}
}, "DESC");
t1.start();
t2.start();
}
}
验证
javac DeadLock.java
java DeadLock
jps
jstack
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/5244fde11bfa5a2a8470ed67244ee180.png)