您的位置:首页 > 编程语言 > Java开发

jps,jstack检测java死锁

2016-03-10 16:05 204 查看
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
* Hello world!
*/
public class App {
public static void main(String[] args) throws InterruptedException {
System.out.println("Hello World!");
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 !!!");
}

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();
}
}
}
}
}

  

 



 

  

2014-07-14 14:19:12
Full thread dump Java HotSpot(TM) 64-Bit Server VM (23.3-b01 mixed mode):

"RMI TCP Connection(5)-192.168.3.100" daemon prio=6 tid=0x000000000d6ed800 nid=0x5374 in Object.wait() [0x000000000ea1d000]
java.lang.Thread.State: TIMED_WAITING (on object monitor)
at java.lang.Object.wait(Native Method)
- waiting on <0x00000007d65fb068> (a com.sun.jmx.remote.internal.ArrayNotificationBuffer)
at com.sun.jmx.remote.internal.ArrayNotificationBuffer.fetchNotifications(Unknown Source)
- locked <0x00000007d65fb068> (a com.sun.jmx.remote.internal.ArrayNotificationBuffer)
at com.sun.jmx.remote.internal.ArrayNotificationBuffer$ShareBuffer.fetchNotifications(Unknown Source)
at com.sun.jmx.remote.internal.ServerNotifForwarder.fetchNotifs(Unknown Source)
at javax.management.remote.rmi.RMIConnectionImpl$2.run(Unknown Source)
at javax.management.remote.rmi.RMIConnectionImpl$2.run(Unknown Source)
at javax.management.remote.rmi.RMIConnectionImpl.fetchNotifications(Unknown Source)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at sun.rmi.server.UnicastServerRef.dispatch(Unknown Source)
at sun.rmi.transport.Transport$1.run(Unknown Source)
at sun.rmi.transport.Transport$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at sun.rmi.transport.Transport.serviceCall(Unknown Source)
at sun.rmi.transport.tcp.TCPTransport.handleMessages(Unknown Source)
at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run0(Unknown Source)
at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)

"RMI TCP Connection(4)-192.168.3.100" daemon prio=6 tid=0x000000000d6ed000 nid=0x21fc runnable [0x000000000e91e000]
java.lang.Thread.State: RUNNABLE
at java.net.SocketInputStream.socketRead0(Native Method)
at java.net.SocketInputStream.read(Unknown Source)
at java.net.SocketInputStream.read(Unknown Source)
at java.io.BufferedInputStream.fill(Unknown Source)
at java.io.BufferedInputStream.read(Unknown Source)
- locked <0x00000007d68b0830> (a java.io.BufferedInputStream)
at java.io.FilterInputStream.read(Unknown Source)
at sun.rmi.transport.tcp.TCPTransport.handleMessages(Unknown Source)
at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run0(Unknown Source)
at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)

"RMI TCP Connection(3)-192.168.3.100" daemon prio=6 tid=0x000000000d4ac800 nid=0x60 runnable [0x000000000e81e000]
java.lang.Thread.State: RUNNABLE
at java.net.SocketInputStream.socketRead0(Native Method)
at java.net.SocketInputStream.read(Unknown Source)
at java.net.SocketInputStream.read(Unknown Source)
at java.io.BufferedInputStream.fill(Unknown Source)
at java.io.BufferedInputStream.read(Unknown Source)
- locked <0x00000007d680e408> (a java.io.BufferedInputStream)
at java.io.FilterInputStream.read(Unknown Source)
at sun.rmi.transport.tcp.TCPTransport.handleMessages(Unknown Source)
at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run0(Unknown Source)
at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)

"JMX server connection timeout 15" daemon prio=6 tid=0x000000000d44b800 nid=0x6184 in Object.wait() [0x000000000e61f000]
java.lang.Thread.State: TIMED_WAITING (on object monitor)
at java.lang.Object.wait(Native Method)
- waiting on <0x00000007d63eee88> (a [I)
at com.sun.jmx.remote.internal.ServerCommunicatorAdmin$Timeout.run(Unknown Source)
- locked <0x00000007d63eee88> (a [I)
at java.lang.Thread.run(Unknown Source)

"RMI Scheduler(0)" daemon prio=6 tid=0x000000000d44a000 nid=0x4c60 waiting on condition [0x000000000e51e000]
java.lang.Thread.State: TIMED_WAITING (parking)
at sun.misc.Unsafe.park(Native Method)
- parking to wait for  <0x00000007d61dc568> (a java.util.concurrent.locks.AbstractQueuedSynchronizer$ConditionObject)
at java.util.concurrent.locks.LockSupport.parkNanos(Unknown Source)
at java.util.concurrent.locks.AbstractQueuedSynchronizer$ConditionObject.awaitNanos(Unknown Source)
at java.util.concurrent.ScheduledThreadPoolExecutor$DelayedWorkQueue.take(Unknown Source)
at java.util.concurrent.ScheduledThreadPoolExecutor$DelayedWorkQueue.take(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor.getTask(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)

"RMI TCP Accept-0" daemon prio=6 tid=0x000000000d71b000 nid=0x5d04 runnable [0x000000000e31e000]
java.lang.Thread.State: RUNNABLE
at java.net.DualStackPlainSocketImpl.accept0(Native Method)
at java.net.DualStackPlainSocketImpl.socketAccept(Unknown Source)
at java.net.AbstractPlainSocketImpl.accept(Unknown Source)
at java.net.PlainSocketImpl.accept(Unknown Source)
- locked <0x00000007d61ea968> (a java.net.SocksSocketImpl)
at java.net.ServerSocket.implAccept(Unknown Source)
at java.net.ServerSocket.accept(Unknown Source)
at sun.management.jmxremote.LocalRMIServerSocketFactory$1.accept(Unknown Source)
at sun.rmi.transport.tcp.TCPTransport$AcceptLoop.executeAcceptLoop(Unknown Source)
at sun.rmi.transport.tcp.TCPTransport$AcceptLoop.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)

"pool-1-thread-2" prio=6 tid=0x000000000b866800 nid=0x2004 waiting for monitor entry [0x000000000d3cf000]
java.lang.Thread.State: BLOCKED (on object monitor)
at App$DeadThread2.run(App.java:72)
- waiting to lock <0x00000007d5ca5c18> (a [B)
- locked <0x00000007d5ca5c28> (a [B)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)

"pool-1-thread-1" prio=6 tid=0x000000000b861000 nid=0x1814 waiting for monitor entry [0x000000000d2cf000]
java.lang.Thread.State: BLOCKED (on object monitor)
at App$DeadThread1.run(App.java:43)
- waiting to lock <0x00000007d5ca5c28> (a [B)
- locked <0x00000007d5ca5c18> (a [B)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)

"Service Thread" daemon prio=6 tid=0x000000000b84a000 nid=0x40b4 runnable [0x0000000000000000]
java.lang.Thread.State: RUNNABLE

"C2 CompilerThread1" daemon prio=10 tid=0x000000000b83a800 nid=0x4a24 waiting on condition [0x0000000000000000]
java.lang.Thread.State: RUNNABLE

"C2 CompilerThread0" daemon prio=10 tid=0x000000000b830800 nid=0x4da0 waiting on condition [0x0000000000000000]
java.lang.Thread.State: RUNNABLE

"Attach Listener" daemon prio=10 tid=0x000000000b82f800 nid=0x3744 waiting on condition [0x0000000000000000]
java.lang.Thread.State: RUNNABLE

"Signal Dispatcher" daemon prio=10 tid=0x000000000b82c000 nid=0x5bf4 runnable [0x0000000000000000]
java.lang.Thread.State: RUNNABLE

"Finalizer" daemon prio=8 tid=0x000000000b7a6800 nid=0x49ec in Object.wait() [0x000000000cbcf000]
java.lang.Thread.State: WAITING (on object monitor)
at java.lang.Object.wait(Native Method)
- waiting on <0x00000007d5c05798> (a java.lang.ref.ReferenceQueue$Lock)
at java.lang.ref.ReferenceQueue.remove(Unknown Source)
- locked <0x00000007d5c05798> (a java.lang.ref.ReferenceQueue$Lock)
at java.lang.ref.ReferenceQueue.remove(Unknown Source)
at java.lang.ref.Finalizer$FinalizerThread.run(Unknown Source)

"Reference Handler" daemon prio=10 tid=0x000000000b79d800 nid=0x4c0c in Object.wait() [0x000000000cace000]
java.lang.Thread.State: WAITING (on object monitor)
at java.lang.Object.wait(Native Method)
- waiting on <0x00000007d5c05320> (a java.lang.ref.Reference$Lock)
at java.lang.Object.wait(Object.java:503)
at java.lang.ref.Reference$ReferenceHandler.run(Unknown Source)
- locked <0x00000007d5c05320> (a java.lang.ref.Reference$Lock)

"main" prio=6 tid=0x0000000000c5e000 nid=0x3300 waiting on condition [0x00000000024bf000]
java.lang.Thread.State: WAITING (parking)
at sun.misc.Unsafe.park(Native Method)
- parking to wait for  <0x00000007d5ca5f90> (a java.util.concurrent.CountDownLatch$Sync)
at java.util.concurrent.locks.LockSupport.park(Unknown Source)
at java.util.concurrent.locks.AbstractQueuedSynchronizer.parkAndCheckInterrupt(Unknown Source)
at java.util.concurrent.locks.AbstractQueuedSynchronizer.doAcquireSharedInterruptibly(Unknown Source)
at java.util.concurrent.locks.AbstractQueuedSynchronizer.acquireSharedInterruptibly(Unknown Source)
at java.util.concurrent.CountDownLatch.await(Unknown Source)
at App.main(App.java:17)

"VM Thread" prio=10 tid=0x000000000b790000 nid=0x2c48 runnable

"GC task thread#0 (ParallelGC)" prio=6 tid=0x000000000289d800 nid=0x2584 runnable

"GC task thread#1 (ParallelGC)" prio=6 tid=0x000000000289f000 nid=0x4e5c runnable

"GC task thread#2 (ParallelGC)" prio=6 tid=0x00000000028a1000 nid=0x7584 runnable

"GC task thread#3 (ParallelGC)" prio=6 tid=0x00000000028a3000 nid=0x24d0 runnable

"VM Periodic Task Thread" prio=10 tid=0x000000000b858800 nid=0x41a4 waiting on condition

JNI global references: 231

Found one Java-level deadlock:
=============================
"pool-1-thread-2":
waiting to lock monitor 0x000000000b8680e8 (object 0x00000007d5ca5c18, a [B),
which is held by "pool-1-thread-1"
"pool-1-thread-1":
waiting to lock monitor 0x000000000b7a62b0 (object 0x00000007d5ca5c28, a [B),
which is held by "pool-1-thread-2"

Java stack information for the threads listed above:
===================================================
"pool-1-thread-2":
at App$DeadThread2.run(App.java:72)
- waiting to lock <0x00000007d5ca5c18> (a [B)
- locked <0x00000007d5ca5c28> (a [B)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
"pool-1-thread-1":
at App$DeadThread1.run(App.java:43)
- waiting to lock <0x00000007d5ca5c28> (a [B)
- locked <0x00000007d5ca5c18> (a [B)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)

Found 1 deadlock.

   使用图形工具 jconsole 同样可以检测到: 

 



 

 

 



 

  



大小: 2.2 KB



大小: 34.8 KB



大小: 49.8 KB

查看图片附件
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: