码迷,mamicode.com
首页 > 编程语言 > 详细

Java 死锁诊断 -- 线程转储

时间:2015-01-27 12:43:21      阅读:482      评论:0      收藏:0      [点我收藏+]

标签:

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 死锁诊断 -- 线程转储

标签:

原文地址:http://www.cnblogs.com/mjorcen/p/4252252.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!