java线程转储

java的线程转储可以被定义为JVM中在某一个给定的时刻运行的所有线程的快照。一个线程转储可能包含一个单独的线程或者多个线程。在多线程环境中,比如J2EE应用服务器,将会有许多线程和线程组。每一个线程都有它自己的调用堆栈,在一个给定时刻,表现为一个独立功能。线程转储将会提供JVM中所有线程的堆栈信息,对于特定的线程也会给出更多信息。

java虚拟机进程和java线程

java虚拟机,或者称为JVM,是一个操作系统级别的进程。java线程是JVM进程的子进程或者轻量级进程(Solar中的叫法)。

生成java线程转储

线程转储可以通过向JVM进程发送一个SIGQUIT信号来生成。有两种不同方式来向进程发送这个信号:

在Unix中,使用“kill -3<pid>”命令,pid表示JVM进程的ID。

在Windows中,在JVM运行时按下CTRL+BREAK键 ,或者使用  jstack 来生成。

[fox]:jstack jstack用于打印出给定的java进程ID或core file或远程调试服务的Java堆栈信息。

Java线程状态

每一个java线程总是处于其生命周期的四个状态之一。

Runnable-线程正在运行,或者准备好获取CPU时间后运行。JRockit线程转储中把这种状态当做Active。

Waiting on Monitor-线程休眠,或者在等待一个对象,或者等待被其他线程唤醒。在线程对象中调用sleep()方法,或者在一个对象中调用wait()方法时就会有这种情况发生。

举个例子,在WebLogc服务器中,空闲的执行线程处于这种状态,他们会一直等待直到一个Socket reader线程有新的任务才唤醒他们。堆栈信息就会如下所示:

"ExecuteThread: '2' for queue: 'weblogic.admin.RMI'" daemon prio=5 tid=0x1752F040 nid=0x180c in Object.wait() [1887f000..1887fd8c]
at java.lang.Object.wait(Native Method) waiting on <04134D98> (a weblogic.kernel.ExecuteThread)
at java.lang.Object.wait(Object.java:426)
at weblogic.kernel.ExecuteThread.waitForRequest(ExecuteThread.java:126)
locked <04134D98> (a weblogic.kernel.ExecuteThread)
at weblogic.kernel.ExecuteThread.run(ExecuteThread.java:145)

在某些别的版本的JVM中称这种状态是CW,Object.wait()(像上面那样)。JRockit称之为WAITING。

Waiting for Monitor Entry——线程在等待获取一个对象的锁(其他线程可能持有这个同步锁)。这种情况发生在当两个或者更多线程尝试执行一段同步代码时。注意“锁”总是针对于一个对象而不是针对一个单独的方法。

这种情况的线程的简单堆栈信息如下:

如:

/**
*
*
* @author mjorcen
* @email mjorcen@gmail.com
* @dateTime Jan 27, 2015 10:51:24 AM
* @version 1
*/
public class DeadThread {
private static Object obj1 = new Object();
private static Object obj2 = new Object(); public static void main(String[] args) {
new Thread(new Runnable() { public void run() {
synchronized (obj1) {
try {
Thread.sleep(200);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
synchronized (obj2) {
System.out.println(1);
}
}
}
}, "t-0").start();
new Thread(new Runnable() { public void run() {
synchronized (obj2) {
try {
Thread.sleep(200);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
synchronized (obj1) {
System.out.println(2);
}
}
}
}, "t-1").start(); }
}

  在 jstack 中.

结果:

2015-01-27 10:54:12
Full thread dump Java HotSpot(TM) 64-Bit Server VM (24.55-b03 mixed mode): "RMI TCP Connection(2)-192.168.1.7" daemon prio=6 tid=0x000000000c016000 nid=0x1030 runnable [0x000000000d73e000]
java.lang.Thread.State: RUNNABLE
at java.net.SocketInputStream.socketRead0(Native Method)
at java.net.SocketInputStream.read(SocketInputStream.java:152)
at java.net.SocketInputStream.read(SocketInputStream.java:122)
at java.io.BufferedInputStream.fill(BufferedInputStream.java:235)
at java.io.BufferedInputStream.read(BufferedInputStream.java:254)
- locked <0x00000007d73f8200> (a java.io.BufferedInputStream)
at java.io.FilterInputStream.read(FilterInputStream.java:83)
at sun.rmi.transport.tcp.TCPTransport.handleMessages(TCPTransport.java:538)
at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run0(TCPTransport.java:811)
at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run(TCPTransport.java:670)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
at java.lang.Thread.run(Thread.java:745) Locked ownable synchronizers:
- <0x00000007d6f99578> (a java.util.concurrent.ThreadPoolExecutor$Worker) "JMX server connection timeout 16" daemon prio=6 tid=0x000000000bedc800 nid=0x1bc8 in Object.wait() [0x000000000ce2f000]
java.lang.Thread.State: TIMED_WAITING (on object monitor)
at java.lang.Object.wait(Native Method)
- waiting on <0x00000007d712d470> (a [I)
at com.sun.jmx.remote.internal.ServerCommunicatorAdmin$Timeout.run(ServerCommunicatorAdmin.java:168)
- locked <0x00000007d712d470> (a [I)
at java.lang.Thread.run(Thread.java:745) Locked ownable synchronizers:
- None "RMI Scheduler(0)" daemon prio=6 tid=0x000000000bedc000 nid=0xa08 waiting on condition [0x000000000cc2e000]
java.lang.Thread.State: TIMED_WAITING (parking)
at sun.misc.Unsafe.park(Native Method)
- parking to wait for <0x00000007d6ef7798> (a java.util.concurrent.locks.AbstractQueuedSynchronizer$ConditionObject)
at java.util.concurrent.locks.LockSupport.parkNanos(LockSupport.java:226)
at java.util.concurrent.locks.AbstractQueuedSynchronizer$ConditionObject.awaitNanos(AbstractQueuedSynchronizer.java:2082)
at java.util.concurrent.ScheduledThreadPoolExecutor$DelayedWorkQueue.take(ScheduledThreadPoolExecutor.java:1090)
at java.util.concurrent.ScheduledThreadPoolExecutor$DelayedWorkQueue.take(ScheduledThreadPoolExecutor.java:807)
at java.util.concurrent.ThreadPoolExecutor.getTask(ThreadPoolExecutor.java:1068)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1130)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
at java.lang.Thread.run(Thread.java:745) Locked ownable synchronizers:
- None "RMI TCP Connection(1)-192.168.1.7" daemon prio=6 tid=0x000000000bf09800 nid=0x16e0 runnable [0x000000000cb0f000]
java.lang.Thread.State: RUNNABLE
at java.net.SocketInputStream.socketRead0(Native Method)
at java.net.SocketInputStream.read(SocketInputStream.java:152)
at java.net.SocketInputStream.read(SocketInputStream.java:122)
at java.io.BufferedInputStream.fill(BufferedInputStream.java:235)
at java.io.BufferedInputStream.read(BufferedInputStream.java:254)
- locked <0x00000007d70d2dd0> (a java.io.BufferedInputStream)
at java.io.FilterInputStream.read(FilterInputStream.java:83)
at sun.rmi.transport.tcp.TCPTransport.handleMessages(TCPTransport.java:538)
at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run0(TCPTransport.java:811)
at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run(TCPTransport.java:670)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
at java.lang.Thread.run(Thread.java:745) Locked ownable synchronizers:
- <0x00000007d6f971f0> (a java.util.concurrent.ThreadPoolExecutor$Worker) "RMI TCP Accept-0" daemon prio=6 tid=0x000000000bf81800 nid=0x15c runnable [0x000000000c93f000]
java.lang.Thread.State: RUNNABLE
at java.net.DualStackPlainSocketImpl.accept0(Native Method)
at java.net.DualStackPlainSocketImpl.socketAccept(DualStackPlainSocketImpl.java:131)
at java.net.AbstractPlainSocketImpl.accept(AbstractPlainSocketImpl.java:398)
at java.net.PlainSocketImpl.accept(PlainSocketImpl.java:198)
- locked <0x00000007d6f07c40> (a java.net.SocksSocketImpl)
at java.net.ServerSocket.implAccept(ServerSocket.java:530)
at java.net.ServerSocket.accept(ServerSocket.java:498)
at sun.management.jmxremote.LocalRMIServerSocketFactory$1.accept(LocalRMIServerSocketFactory.java:52)
at sun.rmi.transport.tcp.TCPTransport$AcceptLoop.executeAcceptLoop(TCPTransport.java:388)
at sun.rmi.transport.tcp.TCPTransport$AcceptLoop.run(TCPTransport.java:360)
at java.lang.Thread.run(Thread.java:745) Locked ownable synchronizers:
- None "DestroyJavaVM" prio=6 tid=0x000000000027d800 nid=0x1b0c waiting on condition [0x0000000000000000]
java.lang.Thread.State: RUNNABLE Locked ownable synchronizers:
- None "Thread-1" prio=6 tid=0x000000000be08800 nid=0x1864 waiting for monitor entry [0x000000000c34f000]
java.lang.Thread.State: BLOCKED (on object monitor)
at mjorcen.nio.test.DeadThread$2.run(DeadThread.java:48)
- waiting to lock <0x00000007d6b1d530> (a java.lang.Object)
- locked <0x00000007d6b1d540> (a java.lang.Object)
at java.lang.Thread.run(Thread.java:745) Locked ownable synchronizers:
- None "Thread-0" prio=6 tid=0x000000000a74d800 nid=0x196c waiting for monitor entry [0x000000000bdff000]
java.lang.Thread.State: BLOCKED (on object monitor)
at mjorcen.nio.test.DeadThread$1.run(DeadThread.java:32)
- waiting to lock <0x00000007d6b1d540> (a java.lang.Object)
- locked <0x00000007d6b1d530> (a java.lang.Object)
at java.lang.Thread.run(Thread.java:745) Locked ownable synchronizers:
- None "Service Thread" daemon prio=6 tid=0x000000000a741000 nid=0x1aec runnable [0x0000000000000000]
java.lang.Thread.State: RUNNABLE Locked ownable synchronizers:
- None "C2 CompilerThread1" daemon prio=10 tid=0x000000000a73c800 nid=0x1bd8 waiting on condition [0x0000000000000000]
java.lang.Thread.State: RUNNABLE Locked ownable synchronizers:
- None "C2 CompilerThread0" daemon prio=10 tid=0x000000000a72b800 nid=0xba0 waiting on condition [0x0000000000000000]
java.lang.Thread.State: RUNNABLE Locked ownable synchronizers:
- None "Attach Listener" daemon prio=10 tid=0x000000000a72a800 nid=0x12d0 waiting on condition [0x0000000000000000]
java.lang.Thread.State: RUNNABLE Locked ownable synchronizers:
- None "Signal Dispatcher" daemon prio=10 tid=0x000000000a729800 nid=0x5d4 runnable [0x0000000000000000]
java.lang.Thread.State: RUNNABLE Locked ownable synchronizers:
- None "Finalizer" daemon prio=8 tid=0x00000000024cf800 nid=0x192c in Object.wait() [0x000000000b6ef000]
java.lang.Thread.State: WAITING (on object monitor)
at java.lang.Object.wait(Native Method)
- waiting on <0x00000007d6a85568> (a java.lang.ref.ReferenceQueue$Lock)
at java.lang.ref.ReferenceQueue.remove(ReferenceQueue.java:135)
- locked <0x00000007d6a85568> (a java.lang.ref.ReferenceQueue$Lock)
at java.lang.ref.ReferenceQueue.remove(ReferenceQueue.java:151)
at java.lang.ref.Finalizer$FinalizerThread.run(Finalizer.java:189) Locked ownable synchronizers:
- None "Reference Handler" daemon prio=10 tid=0x00000000024cb800 nid=0xae4 in Object.wait() [0x000000000b57f000]
java.lang.Thread.State: WAITING (on object monitor)
at java.lang.Object.wait(Native Method)
- waiting on <0x00000007d6a850f0> (a java.lang.ref.Reference$Lock)
at java.lang.Object.wait(Object.java:503)
at java.lang.ref.Reference$ReferenceHandler.run(Reference.java:133)
- locked <0x00000007d6a850f0> (a java.lang.ref.Reference$Lock) Locked ownable synchronizers:
- None "VM Thread" prio=10 tid=0x000000000a6b2000 nid=0x1194 runnable "GC task thread#0 (ParallelGC)" prio=6 tid=0x0000000002404800 nid=0x1a74 runnable "GC task thread#1 (ParallelGC)" prio=6 tid=0x0000000002406000 nid=0x1a7c runnable "VM Periodic Task Thread" prio=10 tid=0x000000000a74a000 nid=0x13fc waiting on condition JNI global references: 150 Found one Java-level deadlock:
=============================
"Thread-1":
waiting to lock monitor 0x000000000a6ba6d8 (object 0x00000007d6b1d530, a java.lang.Object),
which is held by "Thread-0"
"Thread-0":
waiting to lock monitor 0x000000000a6b9238 (object 0x00000007d6b1d540, a java.lang.Object),
which is held by "Thread-1" Java stack information for the threads listed above:
===================================================
"Thread-1":
at mjorcen.nio.test.DeadThread$2.run(DeadThread.java:48)
- waiting to lock <0x00000007d6b1d530> (a java.lang.Object)
- locked <0x00000007d6b1d540> (a java.lang.Object)
at java.lang.Thread.run(Thread.java:745)
"Thread-0":
at mjorcen.nio.test.DeadThread$1.run(DeadThread.java:32)
- waiting to lock <0x00000007d6b1d540> (a java.lang.Object)
- locked <0x00000007d6b1d530> (a java.lang.Object)
at java.lang.Thread.run(Thread.java:745) Found 1 deadlock.

Java 死锁诊断 -- 线程转储的更多相关文章

  1. Java并发之线程转储

    一.java线程转储 java的线程转储可以被定义为JVM中在某一个给定的时刻运行的所有线程的快照.一个线程转储可能包含一个单独的线程或者多个线程.在多线程环境中,比如J2EE应用服务器,将会有许多线 ...

  2. Java 线程转储 [转]

    http://www.oschina.net/translate/java-thread-dump java线程转储 java的线程转储可以被定义为JVM中在某一个给定的时刻运行的所有线程的快照.一个 ...

  3. Java 线程转储

    软件维护是一个枯燥而又有挑战性的工作.只要软件功能符合预期,那么这个工作就是好的.设想一个这样的情景,你的电话半夜也一直在响(这不是一个令人愉快的感受,是吧?)任何软件系统,无论它当初是被设计的多好, ...

  4. 获取Java线程转储的常用方法

    1. 线程转储简介 线程转储(Thread Dump)就是JVM中所有线程状态信息的一次快照. 线程转储一般使用文本格式, 可以将其保存到文本文件中, 然后人工查看和分析, 或者使用工具/API自动分 ...

  5. JAVA之旅(十四)——静态同步函数的锁是class对象,多线程的单例设计模式,死锁,线程中的通讯以及通讯所带来的安全隐患,等待唤醒机制

    JAVA之旅(十四)--静态同步函数的锁是class对象,多线程的单例设计模式,死锁,线程中的通讯以及通讯所带来的安全隐患,等待唤醒机制 JAVA之旅,一路有你,加油! 一.静态同步函数的锁是clas ...

  6. 【Java】线程转储分析 ThreadDump

    [[TOC]] 通过分析 ThreadDump 来查询Java程序运行情况 获取线程转储文件 有多种方式可以获取转储文件,可参考链接HOW TO TAKE THREAD DUMPS? – 8 OPTI ...

  7. java多线程之 ---- 线程死锁

    java多线程之线程死锁 产生死锁的主要原因: 由于系统资源不足. 进程执行推进的顺序不合适. 资源分配不当等. 假设系统资源充足.进程的资源请求都可以得到满足,死锁出现的可能性就非常低.否则就会因争 ...

  8. Java基础_死锁、线程组、定时器Timer

    一.死锁问题: 死锁是这样一种情形:多个线程同时被阻塞,它们中的一个或者全部都在等待某个资源被释放.由于线程被无限期地阻塞,因此程序不可能正常终止. 比如,线程一需要第一把所,此时锁处于空闲状态,给了 ...

  9. Java虚拟机性能管理神器 - VisualVM(9) 排查JAVA应用程序线程死锁【转】

    Java虚拟机性能管理神器 - VisualVM(9) 排查JAVA应用程序线程死锁[转] 标签: javajvm监控工具性能优化 2015-03-11 19:59 1948人阅读 评论(0) 收藏  ...

随机推荐

  1. 如何在某个按钮上触发 bootstarp Modal 组件

    <?php Modal::begin([ 'id'=>'myModal', 'header' => '<h2>标题</h2>']); echo '内容'; M ...

  2. c#中判断对象为空的几种方式(字符串等)

    (1)先了解几个与空类型相关的关键字和对象  Null : 关键字表示不引用任何对象的空引用,它是所有引用类型变量的默认值,在2.0版本之前也就只有引用变量类型可以为null,如(string a=n ...

  3. memcached for windows 修改端口和最大内存

    解压后只要在命令窗口中输入下面命令c:\memcached\memcached.exe -d install  就可以把memcached安装为windows服务了. 启动该服务后,memcached ...

  4. ES6还是ES2015?

    遇到了一个困惑   原来称作es6的现在突然变成es2015 了 原因是这个事ecma-262 的第六次变更,所有以前按照惯例称为es6. 但是为了更小版本频繁发布,现在 标准叫法是:  esmasc ...

  5. php安装redis扩展连接redis服务器

    扩展包的下载地址:https://github.com/nicolasff/phpredis/archive/2.2.4.tar.gz tar xf phpredis-2.2.4.tar.gz cd ...

  6. ubuntu 12.04 LTS(64位)安装apache2

    在网上找了很多文章,说法不一,不知道应该用哪种方法,后来想就用其中最简单的吧,装不好再卸了重装, 然后就...装上了,用的是tar包源码安装,下面是步骤 1.去官网下载apache2.2.25源码包, ...

  7. 12)Java Constructor

    Constructor        构造器Constructor不能被继承,因此不能重写Overriding,但可以被重载Overloading.     构造器用来确保每个对象都会得到初始化.当对 ...

  8. Kafka入门学习随记(二)

    ====Kafka消费者模型 参考博客:http://www.tuicool.com/articles/fI7J3m --分区消费模型 分区消费架构图 图中kafka集群有两台服务器(Server), ...

  9. 第十九章 数据访问(In .net4.5) 之 处理数据

    1. 概述 本章介绍 数据库.Json和Xml.web services 三种介质上的数据操作. 2. 主要内容 2.1 数据库 ① 建立连接 .net平台中的数据连接类都继承自DbConnectio ...

  10. jquery.tmpl 用法(附上详细案例)

    js的模板引擎就和服务端的差不多,都是更好更快的拼接html用于显示,我参考了文章:http://www.cnblogs.com/zhuzhiyuan/p/3510175.html tmpl常用标签 ...