Thread Dump分析

本文详细介绍了如何获取和分析Thread Dump,包括线程的状态如Runnable、Wait on condition、Waiting for Monitor Entry和Object.wait()的解释,并讨论了在CPU高负载、响应慢等问题下的线程排查策略,以及如何查找占用CPU最多的线程信息。
摘要由CSDN通过智能技术生成

 thread dump获取

1. 发送信号
* In Unix, use "kill -3 <pid>" where pid is the Process ID of the JVM.( kill 信号列表
* In Windows, press CTRL+BREAK on the window where the JVM is running.

2. jstack -l pid. 

(1). jstack入口

public class JStack {
            public static void main(String[] args) throws Exception {
               ...
                    String pid = args[optionCount];
                    String params[];
                    if (locks) {
                        params = new String[] { "-l" };
                    } else {
                        params = new String[0];
                    }
                    runThreadDump(pid, params);
                ...
            }
            // Attach to pid and perform a thread dump 
            private static void runThreadDump(String pid, String args[])
                    throws Exception {
                VirtualMachine vm = null;
                try {
                    vm = VirtualMachine.attach(pid);
                } catch (Exception x) {
                   ...
                }
                // Cast to HotSpotVirtualMachine as this is implementation specific
                // method.
                InputStream in = ((HotSpotVirtualMachine) vm)
                        .remoteDataDump((Object[]) args);
                // read to EOF and just print output 
                byte b[] = new byte[256];
                int n;
                do {
                    n = in.read(b);
                    if (n > 0) {
                        String s = new String(b, 0, n, "UTF-8");
                        System.out.print(s);
                    }
                } while (n > 0);
                in.close();
                vm.detach();
            }
(2).执行方式
LinuxVirtualMachine.execute
/**
    * Execute the given command in the target VM.
    */
   @Override
   InputStream execute(String cmd, Object... args) throws AgentLoadException, IOException
   {
      ...
      int s = socket();
      // connect to target VM
      try {
         connect(s, p);
      }
      catch (IOException x) {
         close(s);
         throw x;
      }
      IOException ioe = null;
      // connected - write request,发送command到target j
  • 0
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
Java Thread dump是Java应用程序运行时的快照,它记录了Java虚拟机中所有线程的状态和调用栈信息。通过分析Thread dump可以帮助我们解决Java应用程序出现的性能问题和死锁等问题。 以下是Java Thread dump分析的步骤: 1. 获取Thread dump 可以使用jstack命令来获取Java应用程序的Thread dump。例如,可以在Linux系统中使用以下命令来获取正在运行的Java进程的Thread dump: ``` jstack <pid> > thread-dump.txt ``` 其中,<pid>是Java应用程序的进程ID。 2. 分析Thread dump Thread dump通常是一个文本文件,可以使用文本编辑器打开并查看。在Thread dump中,每个线程都有自己的ID和状态,例如运行、睡眠等。另外,每个线程还有自己的调用栈信息,它们显示了该线程正在执行的方法以及它们的调用关系。 通过分析Thread dump,我们可以找到可能存在的性能问题和死锁。例如,可以查找正在运行的线程中占用CPU时间最长的线程,并分析其调用栈信息,找出可能存在的性能问题。另外,可以查找睡眠状态的线程,并分析它们的调用栈信息,找出可能存在的死锁问题。 3. 解决问题 根据Thread dump分析结果,我们可以采取相应的措施来解决问题。例如,可以对性能问题进行优化,例如缓存数据、减少I/O操作等。对于死锁问题,可以使用Java提供的synchronized关键字来保证线程同步,或者使用Lock接口提供的锁来实现更灵活的线程同步。 综上所述,Java Thread dump分析是解决Java应用程序性能问题和死锁等问题的重要手段。通过分析Thread dump,我们可以找到问题的根源,并采取相应的措施来解决问题。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值