java线程状态——死锁

参考:

java 线程状态

java 线程状态——情况续说

java线程状态——java线程状态图

死锁,概念有很多,可以google

现在列出死锁的模拟代码和jstack的输出

一方面是理解死锁,一方面是学习jstack找出死锁,进行代码调优

import java.util.concurrent.TimeUnit;

public class e
{
        static class DeadT implements Runnable
        {
                private Object lock1;
                private Object lock2;
                private String name;
                private String lockname1;
                private String lockname2;

                public DeadT(Object lock1,Object lock2,String name,String lockname1,String lockname2)
                {
                        this.lock1 = lock1;
                        this.lock2 = lock2;
                        this.name = name;
                        this.lockname1 = lockname1;
                        this.lockname2 = lockname2;
                }

                public void run()
                {
                        System.out.println(name+" acquiring lock"+lockname1+"... ");
                        synchronized(lock1)
                        {
                                System.out.println(name + " acquired lock"+lockname1+"... ");
                                work();
                                System.out.println(name + " acquiring lock"+lockname2+"... ");
                                synchronized(lock2)
                                {
                                        System.out.println(name + " acquired lock"+lockname2+"... ");
                                }
                        }


                }//end run

                public void work()
                {
                        try
                        {
                                TimeUnit.SECONDS.sleep(8);
                        } catch(Exception e)
                        {
                                e.printStackTrace();
                        }
                }
        }

        public static void main(String[] args)
        {
                Object lock1 = new Object();
                Object lock2 = new Object();

                Thread t1 = new Thread(new DeadT(lock1,lock2,"t1","1","2"));
                Thread t2 = new Thread(new DeadT(lock2,lock1,"t2","2","1"));

                t1.setName("t1");
                t2.setName("t2");

                t1.start();
                try
                {
                        TimeUnit.SECONDS.sleep(2);
                }
                catch(Exception e)
                {
                        e.printStackTrace();
                }
                t2.start();
                try
                {
                        TimeUnit.SECONDS.sleep(2);
                }
                catch(Exception e)
                {
                        e.printStackTrace();
                }
                System.out.println("main finished!");
        }

jstack输出如下:

2017-02-14 14:13:20
Full thread dump Java HotSpot(TM) 64-Bit Server VM (24.80-b11 mixed mode):

   java.lang.Thread.State: RUNNABLE

"DestroyJavaVM" prio=10 tid=0x00007f8c68008800 nid=0x37a4 waiting on condition [0x0000000000000000]
   java.lang.Thread.State: RUNNABLE

"t2" prio=10 tid=0x00007f8c680bb000 nid=0x37b6 waiting for monitor entry [0x00007f8c55cd4000]
   java.lang.Thread.State: BLOCKED (on object monitor)
        at e$DeadT.run(e.java:33)
        - waiting to lock <0x00000007d704aa10> (a java.lang.Object)
        - locked <0x00000007d704aa20> (a java.lang.Object)
        at java.lang.Thread.run(Thread.java:745)

"t1" prio=10 tid=0x00007f8c680b9000 nid=0x37b5 waiting for monitor entry [0x00007f8c55dd5000]
   java.lang.Thread.State: BLOCKED (on object monitor)
        at e$DeadT.run(e.java:33)
        - waiting to lock <0x00000007d704aa20> (a java.lang.Object)
        - locked <0x00000007d704aa10> (a java.lang.Object)
        at java.lang.Thread.run(Thread.java:745)

   java.lang.Thread.State: RUNNABLE

   java.lang.Thread.State: RUNNABLE

   java.lang.Thread.State: RUNNABLE

   java.lang.Thread.State: RUNNABLE

   java.lang.Thread.State: WAITING (on object monitor)
        at java.lang.Object.wait(Native Method)
        - waiting on <0x00000007d7004858> (a java.lang.ref.ReferenceQueue$Lock)
        at java.lang.ref.ReferenceQueue.remove(ReferenceQueue.java:135)
        - locked <0x00000007d7004858> (a java.lang.ref.ReferenceQueue$Lock)
        at java.lang.ref.ReferenceQueue.remove(ReferenceQueue.java:151)
        at java.lang.ref.Finalizer$FinalizerThread.run(Finalizer.java:209)

   java.lang.Thread.State: WAITING (on object monitor)
        at java.lang.Object.wait(Native Method)
        - waiting on <0x00000007d7004470> (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 <0x00000007d7004470> (a java.lang.ref.Reference$Lock)"GC task thread#0 (ParallelGC)" prio=10 tid=0x00007f8c6801e000 nid=0x37a5 runnable

"GC task thread#1 (ParallelGC)" prio=10 tid=0x00007f8c68020000 nid=0x37a6 runnable

"GC task thread#2 (ParallelGC)" prio=10 tid=0x00007f8c68022000 nid=0x37a7 runnable

"GC task thread#3 (ParallelGC)" prio=10 tid=0x00007f8c68023800 nid=0x37a8 runnable

"GC task thread#4 (ParallelGC)" prio=10 tid=0x00007f8c68025800 nid=0x37a9 runnable

"GC task thread#5 (ParallelGC)" prio=10 tid=0x00007f8c68027800 nid=0x37aa runnable

"GC task thread#6 (ParallelGC)" prio=10 tid=0x00007f8c68029000 nid=0x37ab runnable

"GC task thread#7 (ParallelGC)" prio=10 tid=0x00007f8c6802b000 nid=0x37ac runnable

"VM Periodic Task Thread" prio=10 tid=0x00007f8c680aa000 nid=0x37b4 waiting on condition

JNI global references: 107


Found one Java-level deadlock:
=============================
"t2":
  waiting to lock monitor 0x00007f8c1c006218 (object 0x00000007d704aa10, a java.lang.Object),
  which is held by "t1"
"t1":
  waiting to lock monitor 0x00007f8c1c003988 (object 0x00000007d704aa20, a java.lang.Object),
  which is held by "t2"

Java stack information for the threads listed above:
===================================================
"t2":
        at e$DeadT.run(e.java:33)
        - waiting to lock <0x00000007d704aa10> (a java.lang.Object)
        - locked <0x00000007d704aa20> (a java.lang.Object)
        at java.lang.Thread.run(Thread.java:745)
"t1":
        at e$DeadT.run(e.java:33)
        - waiting to lock <0x00000007d704aa20> (a java.lang.Object)
        - locked <0x00000007d704aa10> (a java.lang.Object)
        at java.lang.Thread.run(Thread.java:745)

Found 1 deadlock.

从jstack输出中可以很明显找到deadlock的字眼,并在最后说出了死锁的条件,请仔细看jstack输出以及代码。

    原文作者:java锁
    原文地址: https://blog.csdn.net/u014783753/article/details/55101918
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞