jstack简单使用,定位死循环、线程阻塞、死锁等问题

package com.zk.netty.user;

import java.lang.management.ManagementFactory;
import java.lang.management.RuntimeMXBean;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.concurrent.*;

import com.alibaba.fastjson.JSONObject;
import com.zk.netty.enums.Code;
import io.netty.util.ThreadDeathWatcher;
import io.netty.util.Version;
import io.netty.util.concurrent.DefaultThreadFactory;
import org.apache.commons.lang.math.RandomUtils;
import org.apache.commons.lang.time.StopWatch;

public class CodeMsgUtil {
    
    public static void main(String args[]){
        try {
            System.out.println(getProcessID());
            ExecutorService executorService = Executors.newFixedThreadPool(2);
            byte[] i = new byte[0];
            byte[] j = new byte[0];
            final CountDownLatch countDownLatch = new CountDownLatch(2);
            executorService.execute(new DeadThread1(i,j,countDownLatch));
            executorService.execute(new DeadThread2(i,j,countDownLatch));
            countDownLatch.await();
            System.out.println("done 1");
        }catch (Exception e){

        }
    }

    public static class DeadThread1 implements Runnable{
        private byte[] i;
        private byte[] j;
        private CountDownLatch countDownLatch;
        public DeadThread1(byte[] i,byte[] j ,CountDownLatch countDownLatch){
            this.i=i;
            this.j=j;
            this.countDownLatch = countDownLatch;
        }

        @Override
        public void run() {
            synchronized (i){
                try {
                    Thread.sleep(1000);
                }catch (InterruptedException e){
                    e.printStackTrace();
                }
                synchronized (j){
                    System.out.println(Thread.currentThread().getName()+" is running");
                    countDownLatch.countDown();
                }
            }
        }
    }

    public static class DeadThread2 implements Runnable{
        private byte[] i;
        private byte[] j;
        private CountDownLatch countDownLatch;
        public DeadThread2(byte[] i,byte[] j ,CountDownLatch countDownLatch){
            this.i=i;
            this.j=j;
            this.countDownLatch = countDownLatch;
        }

        @Override
        public void run() {
            synchronized (j){
                try {
                    Thread.sleep(1000);
                }catch (InterruptedException e){
                    e.printStackTrace();
                }
                synchronized (i){
                    System.out.println(Thread.currentThread().getName()+" is running");
                    countDownLatch.countDown();
                }
            }
        }
    }

    public static final int getProcessID() {
        RuntimeMXBean runtimeMXBean = ManagementFactory.getRuntimeMXBean();
        System.out.println(runtimeMXBean.getName());
        return Integer.valueOf(runtimeMXBean.getName().split("@")[0])
                .intValue();
    }
}

 

上面的代码运行会出现死循环,所以打开cmd,输入jps命令,jps很简单可以直接显示java进程的pid,如下为7732:

输入jstack -l 7732

最后输出

找到了死循环,具体也输出了死循环的地方。接下来自己解决死循环

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值