jstack查看死锁
/**
* 哲学家吃饭问题:
* 获取锁的顺序不一致导致死锁
*/
public class PhilosopherEatingProblem {
static class Philosopher implements Runnable {
private int id;
private final Object chopstick1;
private final Object chopstick2;
private final int EAT_TIMES = 100;
private Random random = new Random();
public Philosopher(int id, Object chopstick1, Object chopstick2) {
this.id = id;
this.chopstick1 = chopstick1;
this.chopstick2 = chopstick2;
}
@Override
public void run() {
for (int i = 0; i < EAT_TIMES; i++) {
if (random.nextInt(2) == 0) {
lockFromLeft();
} else {
lockFromRight();
}
try {
Thread.sleep(random.nextInt(200));
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
private synchronized void lockFromLeft() {
synchronized (chopstick1) {
synchronized (chopstick2) {
System.out.printf("p%s I have got the chopstick and eat.\r\n", this.id);
}
}
}
private synchronized void lockFromRight() {
synchronized (chopstick2) {
synchronized (chopstick1) {
System.out.printf("p%s I have got the chopstick and eat.\r\n", this.id);
}
}
}
}
public static void main(String[] args) throws InterruptedException {
Object chopstick1 = new Object();
Object chopstick2 = new Object();
ExecutorService executor = Executors.newFixedThreadPool(11);
for (int i = 0; i < 11; i++) {
Philosopher philosopher = new Philosopher(i, chopstick1, chopstick2);
executor.submit(philosopher);
}
executor.shutdown();
executor.awaitTermination(120, TimeUnit.SECONDS);
}
}
运行程序, 出现死锁.
使用jps
查看当前java]程序的pid:
C:\Users\Pactera>jps
# 结果如下
11316 Launcher
6868 RemoteMavenServer
8660
10952 GradleDaemon
9768 QuorumPeerMain
10844 Jps
5516 KotlinCompileDaemon
6268 PhilosopherEatingProblem
在cmd运行jstack [pid]
命令.
jstack 6268
部分结果
Found one Java-level deadlock:
=============================
"pool-1-thread-11":
waiting to lock monitor 0x00000000180806a8 (object 0x00000000d5fb5ec0, a java.lang.Object),
which is held by "pool-1-thread-2"
"pool-1-thread-2":
waiting to lock monitor 0x000000001983c788 (object 0x00000000d5fb5eb0, a java.lang.Object),
which is held by "pool-1-thread-4"
"pool-1-thread-4":
waiting to lock monitor 0x00000000180806a8 (object 0x00000000d5fb5ec0, a java.lang.Object),
which is held by "pool-1-thread-2"
Java stack information for the threads listed above:
===================================================
"pool-1-thread-11":
at lab.anoper.exam.lock.PhilosopherEatingProblem$Philosopher.lockFromRight(PhilosopherEatingProblem.java:52)
- waiting to lock <0x00000000d5fb5ec0> (a java.lang.Object)
- locked <0x00000000d5fda7b8> (a lab.anoper.exam.lock.PhilosopherEatingProblem$Philosopher)
at lab.anoper.exam.lock.PhilosopherEatingProblem$Philosopher.run(PhilosopherEatingProblem.java:31)
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
at java.lang.Thread.run(Thread.java:748)
"pool-1-thread-2":
at lab.anoper.exam.lock.PhilosopherEatingProblem$Philosopher.lockFromRight(PhilosopherEatingProblem.java:53)
- waiting to lock <0x00000000d5fb5eb0> (a java.lang.Object)
- locked <0x00000000d5fb5ec0> (a java.lang.Object)
- locked <0x00000000d5fd8a40> (a lab.anoper.exam.lock.PhilosopherEatingProblem$Philosopher)
at lab.anoper.exam.lock.PhilosopherEatingProblem$Philosopher.run(PhilosopherEatingProblem.java:31)
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
at java.lang.Thread.run(Thread.java:748)
"pool-1-thread-4":
at lab.anoper.exam.lock.PhilosopherEatingProblem$Philosopher.lockFromLeft(PhilosopherEatingProblem.java:45)
- waiting to lock <0x00000000d5fb5ec0> (a java.lang.Object)
- locked <0x00000000d5fb5eb0> (a java.lang.Object)
- locked <0x00000000d5fd90e0> (a lab.anoper.exam.lock.PhilosopherEatingProblem$Philosopher)
at lab.anoper.exam.lock.PhilosopherEatingProblem$Philosopher.run(PhilosopherEatingProblem.java:29)
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
at java.lang.Thread.run(Thread.java:748)