jstack命令使用

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)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值