手写java死锁,java命令查看死锁
作者:互联网
类Thread1
package github.write.funding2.Write;
public class Thread1 implements Runnable {
private Object lock1;
private Object lock2;
public Thread1(Object lock1, Object lock2) {
this.lock1 = lock1;
this.lock2 = lock2;
}
@Override
public void run() {
synchronized (lock1){
try {
Thread.sleep(40);
} catch (InterruptedException e) {
e.printStackTrace();
}
synchronized (lock2){
System.out.println(Thread.currentThread().getName());
}
}
}
}
类Thread2
package github.write.funding2.Write;
public class Thread2 implements Runnable {
private Object lock1;
private Object lock2;
public Thread2(Object lock1, Object lock2) {
this.lock1 = lock1;
this.lock2 = lock2;
}
@Override
public void run() {
synchronized (lock2){
try {
Thread.sleep(40);
} catch (InterruptedException e) {
e.printStackTrace();
}
synchronized (lock1){
System.out.println(Thread.currentThread().getName());
}
}
}
}
测试类
package github.write.funding2.Write;
public class LockTest {
public static void main(String[] args) {
Object o1=new Object();
Object o2=new Object();
new Thread(new Thread1(o1,o2),"Thread1").start();
new Thread(new Thread2(o1,o2),"Thread2").start();
}
}
Java命令查看死锁
jps(jvm process status)
F:\GitClone\funding2>jps
12144 Launcher
2784 Jps
11188
11096 LockTest
3160 KotlinCompileDaemon
F:\GitClone\funding2>jstack 11096
Found one Java-level deadlock:
=============================
"Thread1":
waiting to lock monitor 0x000002091a3cb1f8 (object 0x00000000d5c3a7d0, a java.lang.Object),
which is held by "Thread1"
"Thread2":
waiting to lock monitor 0x000002091a3cc698 (object 0x00000000d5c3a7e0, a java.lang.Object),
which is held by "Thread2"
标签:java,Object,lock2,死锁,lock1,Thread2,Thread1,手写,public 来源: https://blog.csdn.net/qq_42453201/article/details/110739834