JAVA 线程死锁,以及linux 命令和jstack 命令 查看线程死锁状态信息

来源:互联网 发布:南宁哪有美工勾刀卖 编辑:程序博客网 时间:2024/05/21 22:36

/*
 * Copyright (C) 2009 The doctor Authors
 * https://github.com/doctorwho1986
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */


/**

 * 
 * @author doctor
 *
 * @date 2014年8月23日 下午8:46:37
 */
public class DeadLock {
/**
* 1、linux 命令 kill -3 pid 对于线程状态观察,eclipse console 会输出
* 2、java 命令 jstack pid 观察线程状态。
* @param args
*/
public static void main(String[] args) {

//得到jvm线程pid,用命令观察线程死锁信息
System.out.println(ManagementFactory.getRuntimeMXBean().getName());
DeadLockAB deadLockAB = new DeadLockAB();
new Thread(new Dead1(deadLockAB)).start();
new Thread(new Dead2(deadLockAB)).start();


}


}


class DeadLockAB{
private Object a = new Object();
private Object b = new Object();

public void callAB() {
synchronized (a) {
System.out.println(Thread.currentThread().getName() + " get a");
synchronized (b) {
System.out.println(Thread.currentThread().getName() + " get b");
}
System.out.println(Thread.currentThread().getName() + " release b");
}
System.out.println(Thread.currentThread().getName() + " release a");
}

public void callBA() {
synchronized (b) {
System.out.println(Thread.currentThread().getName() + "get b");
synchronized (a) {
System.out.println(Thread.currentThread().getName() + " get a");
}
System.out.println(Thread.currentThread().getName() + " release a");
}
System.out.println(Thread.currentThread().getName() + " release b");
}
}


class Dead1 implements Runnable{


private DeadLockAB deadLockAB;

public Dead1(DeadLockAB deadLockAB) {
this.deadLockAB = deadLockAB;
}
@Override
public void run() {
try {
while(true){
deadLockAB.callAB();
TimeUnit.SECONDS.sleep((int)Math.random()*10L);
}

} catch (Exception e) {
e.printStackTrace();
}
}

}




class Dead2 implements Runnable{


private DeadLockAB deadLockAB;

public Dead2(DeadLockAB deadLockAB) {
this.deadLockAB = deadLockAB;
}
@Override
public void run() {
try {
while(true){
deadLockAB.callBA();
TimeUnit.SECONDS.sleep((int)Math.random()*10L);
}

} catch (Exception e) {
e.printStackTrace();
}
}

}


当eclipse console 不再出现信息即死锁状态时候,用注释中的两条命令观察信息。


贴出信息;


[doctor@localhost ~]$ jstack 18773
2014-08-23 21:33:17
Full thread dump Java HotSpot(TM) 64-Bit Server VM (25.11-b03 mixed mode):


"Attach Listener" #12 daemon prio=9 os_prio=0 tid=0x00007fd268001000 nid=0x4982 waiting on condition [0x0000000000000000]
   java.lang.Thread.State: RUNNABLE


"DestroyJavaVM" #11 prio=5 os_prio=0 tid=0x00007fd2a4009800 nid=0x495b waiting on condition [0x0000000000000000]
   java.lang.Thread.State: RUNNABLE


"Thread-1" #10 prio=5 os_prio=0 tid=0x00007fd2a40fc000 nid=0x496b waiting for monitor entry [0x00007fd290aea000]
   java.lang.Thread.State: BLOCKED (on object monitor)
at com.github.jdk.concurrent.DeadLockAB.callBA(DeadLock.java:49)
- waiting to lock <0x00000000d810a108> (a java.lang.Object)
- locked <0x00000000d810a118> (a java.lang.Object)
at com.github.jdk.concurrent.Dead2.run(DeadLock.java:91)
at java.lang.Thread.run(Thread.java:745)


"Thread-0" #9 prio=5 os_prio=0 tid=0x00007fd2a40fa000 nid=0x496a waiting for monitor entry [0x00007fd290beb000]
   java.lang.Thread.State: BLOCKED (on object monitor)
at com.github.jdk.concurrent.DeadLockAB.callAB(DeadLock.java:38)
- waiting to lock <0x00000000d810a118> (a java.lang.Object)
- locked <0x00000000d810a108> (a java.lang.Object)
at com.github.jdk.concurrent.Dead1.run(DeadLock.java:68)
at java.lang.Thread.run(Thread.java:745)


"Service Thread" #8 daemon prio=9 os_prio=0 tid=0x00007fd2a40d7000 nid=0x4968 runnable [0x0000000000000000]
   java.lang.Thread.State: RUNNABLE


"C1 CompilerThread2" #7 daemon prio=9 os_prio=0 tid=0x00007fd2a40b1800 nid=0x4967 waiting on condition [0x0000000000000000]
   java.lang.Thread.State: RUNNABLE


"C2 CompilerThread1" #6 daemon prio=9 os_prio=0 tid=0x00007fd2a40b0000 nid=0x4966 waiting on condition [0x0000000000000000]
   java.lang.Thread.State: RUNNABLE


"C2 CompilerThread0" #5 daemon prio=9 os_prio=0 tid=0x00007fd2a40ad000 nid=0x4965 waiting on condition [0x0000000000000000]
   java.lang.Thread.State: RUNNABLE


"Signal Dispatcher" #4 daemon prio=9 os_prio=0 tid=0x00007fd2a40ab000 nid=0x4964 runnable [0x0000000000000000]
   java.lang.Thread.State: RUNNABLE


"Finalizer" #3 daemon prio=8 os_prio=0 tid=0x00007fd2a407c000 nid=0x4963 in Object.wait() [0x00007fd2a837d000]
   java.lang.Thread.State: WAITING (on object monitor)
at java.lang.Object.wait(Native Method)
- waiting on <0x00000000d81809b0> (a java.lang.ref.ReferenceQueue$Lock)
at java.lang.ref.ReferenceQueue.remove(ReferenceQueue.java:142)
- locked <0x00000000d81809b0> (a java.lang.ref.ReferenceQueue$Lock)
at java.lang.ref.ReferenceQueue.remove(ReferenceQueue.java:158)
at java.lang.ref.Finalizer$FinalizerThread.run(Finalizer.java:209)


"Reference Handler" #2 daemon prio=10 os_prio=0 tid=0x00007fd2a4078000 nid=0x4962 in Object.wait() [0x00007fd2a847e000]
   java.lang.Thread.State: WAITING (on object monitor)
at java.lang.Object.wait(Native Method)
- waiting on <0x00000000d81889d8> (a java.lang.ref.Reference$Lock)
at java.lang.Object.wait(Object.java:502)
at java.lang.ref.Reference$ReferenceHandler.run(Reference.java:157)
- locked <0x00000000d81889d8> (a java.lang.ref.Reference$Lock)


"VM Thread" os_prio=0 tid=0x00007fd2a4072800 nid=0x4961 runnable 


"GC task thread#0 (ParallelGC)" os_prio=0 tid=0x00007fd2a401f000 nid=0x495d runnable 


"GC task thread#1 (ParallelGC)" os_prio=0 tid=0x00007fd2a4020800 nid=0x495e runnable 


"GC task thread#2 (ParallelGC)" os_prio=0 tid=0x00007fd2a4022800 nid=0x495f runnable 


"GC task thread#3 (ParallelGC)" os_prio=0 tid=0x00007fd2a4024000 nid=0x4960 runnable 


"VM Periodic Task Thread" os_prio=0 tid=0x00007fd2a40e1800 nid=0x4969 waiting on condition 


JNI global references: 18




Found one Java-level deadlock:
=============================
"Thread-1":
  waiting to lock monitor 0x00007fd274006168 (object 0x00000000d810a108, a java.lang.Object),
  which is held by "Thread-0"
"Thread-0":
  waiting to lock monitor 0x00007fd274003828 (object 0x00000000d810a118, a java.lang.Object),
  which is held by "Thread-1"


Java stack information for the threads listed above:
===================================================
"Thread-1":
at com.github.jdk.concurrent.DeadLockAB.callBA(DeadLock.java:49)
- waiting to lock <0x00000000d810a108> (a java.lang.Object)
- locked <0x00000000d810a118> (a java.lang.Object)
at com.github.jdk.concurrent.Dead2.run(DeadLock.java:91)
at java.lang.Thread.run(Thread.java:745)
"Thread-0":
at com.github.jdk.concurrent.DeadLockAB.callAB(DeadLock.java:38)
- waiting to lock <0x00000000d810a118> (a java.lang.Object)
- locked <0x00000000d810a108> (a java.lang.Object)
at com.github.jdk.concurrent.Dead1.run(DeadLock.java:68)
at java.lang.Thread.run(Thread.java:745)


Found 1 deadlock.


[doctor@localhost ~]$ 

0 0