page contents

jvm问题分析

Pack 发布于 2020-01-04 15:45
阅读 617
收藏 0
分类:Java开发

为什么程序一直运行,出现了死锁问题,请大神分析一下,dump看不懂,请指点。


public class Foo {

    public Semaphore seam_first_two = new Semaphore(0);


    public Semaphore seam_two_second = new Semaphore(0);


    public Foo() {


    }


    public void first(Runnable printFirst) throws InterruptedException {

        printFirst.run();

        seam_first_two.release();

    }


    public void second(Runnable printSecond) throws InterruptedException {

        seam_first_two.acquire();

        printSecond.run();

        seam_two_second.release();

    }


    public void third(Runnable printThird) throws InterruptedException {

        seam_two_second.acquire();

        printThird.run();

    }



    public static void main(String[] args) throws Exception{


        Runnable r1 = () -> System.out.print("one");


        Runnable r2 = () -> System.out.print("two");


        Runnable r3 = () -> System.out.print("three");


        Foo foo = new Foo();

        foo.second(r2);

        foo.third(r3);

        foo.first(r1);



    }

}

dump信息:


2019-09-20 10:48:58

Full thread dump Java HotSpot(TM) 64-Bit Server VM (25.181-b13 mixed mode):


"Service Thread" #10 daemon prio=9 os_prio=0 tid=0x0000000058fa4000 nid=0x2b88 runnable [0x0000000000000000]

   java.lang.Thread.State: RUNNABLE


"C1 CompilerThread2" #9 daemon prio=9 os_prio=2 tid=0x0000000058f80800 nid=0x2424 waiting on condition [0x0000000000000000]

   java.lang.Thread.State: RUNNABLE


"C2 CompilerThread1" #8 daemon prio=9 os_prio=2 tid=0x0000000058f16800 nid=0x1d48 waiting on condition [0x0000000000000000]

   java.lang.Thread.State: RUNNABLE


"C2 CompilerThread0" #7 daemon prio=9 os_prio=2 tid=0x0000000058f28800 nid=0x2bfc waiting on condition [0x0000000000000000]

   java.lang.Thread.State: RUNNABLE


"Monitor Ctrl-Break" #6 daemon prio=5 os_prio=0 tid=0x0000000058f27000 nid=0x27d4 runnable [0x00000000586fe000]

   java.lang.Thread.State: RUNNABLE

at java.net.SocketInputStream.socketRead0(Native Method)

at java.net.SocketInputStream.socketRead(SocketInputStream.java:116)

at java.net.SocketInputStream.read(SocketInputStream.java:171)

at java.net.SocketInputStream.read(SocketInputStream.java:141)

at sun.nio.cs.StreamDecoder.readBytes(StreamDecoder.java:284)

at sun.nio.cs.StreamDecoder.implRead(StreamDecoder.java:326)

at sun.nio.cs.StreamDecoder.read(StreamDecoder.java:178)

- locked <0x00000000d72bdfa0> (a java.io.InputStreamReader)

at java.io.InputStreamReader.read(InputStreamReader.java:184)

at java.io.BufferedReader.fill(BufferedReader.java:161)

at java.io.BufferedReader.readLine(BufferedReader.java:324)

- locked <0x00000000d72bdfa0> (a java.io.InputStreamReader)

at java.io.BufferedReader.readLine(BufferedReader.java:389)

at com.intellij.rt.execution.application.AppMainV2$1.run(AppMainV2.java:64)


"Attach Listener" #5 daemon prio=5 os_prio=2 tid=0x0000000058c73000 nid=0x278c runnable [0x0000000000000000]

   java.lang.Thread.State: RUNNABLE


"Signal Dispatcher" #4 daemon prio=9 os_prio=2 tid=0x0000000058c72800 nid=0x2894 waiting on condition [0x0000000000000000]

   java.lang.Thread.State: RUNNABLE


"Finalizer" #3 daemon prio=8 os_prio=1 tid=0x00000000576bd800 nid=0x299c in Object.wait() [0x0000000058c5f000]

   java.lang.Thread.State: WAITING (on object monitor)

at java.lang.Object.wait(Native Method)

- waiting on <0x00000000d7008ed0> (a java.lang.ref.ReferenceQueue$Lock)

at java.lang.ref.ReferenceQueue.remove(ReferenceQueue.java:144)

- locked <0x00000000d7008ed0> (a java.lang.ref.ReferenceQueue$Lock)

at java.lang.ref.ReferenceQueue.remove(ReferenceQueue.java:165)

at java.lang.ref.Finalizer$FinalizerThread.run(Finalizer.java:216)


"Reference Handler" #2 daemon prio=10 os_prio=2 tid=0x0000000057676800 nid=0x2980 in Object.wait() [0x0000000058a9e000]

   java.lang.Thread.State: WAITING (on object monitor)

at java.lang.Object.wait(Native Method)

- waiting on <0x00000000d7006bf8> (a java.lang.ref.Reference$Lock)

at java.lang.Object.wait(Object.java:502)

at java.lang.ref.Reference.tryHandlePending(Reference.java:191)

- locked <0x00000000d7006bf8> (a java.lang.ref.Reference$Lock)

at java.lang.ref.Reference$ReferenceHandler.run(Reference.java:153)


"main" #1 prio=5 os_prio=0 tid=0x000000000249b000 nid=0x16fc waiting on condition [0x000000000299f000]

   java.lang.Thread.State: WAITING (parking)

at sun.misc.Unsafe.park(Native Method)

- parking to wait for  <0x00000000d7589f88> (a java.util.concurrent.Semaphore$NonfairSync)

at java.util.concurrent.locks.LockSupport.park(LockSupport.java:175)

at java.util.concurrent.locks.AbstractQueuedSynchronizer.parkAndCheckInterrupt(AbstractQueuedSynchronizer.java:836)

at java.util.concurrent.locks.AbstractQueuedSynchronizer.doAcquireSharedInterruptibly(AbstractQueuedSynchronizer.java:997)

at java.util.concurrent.locks.AbstractQueuedSynchronizer.acquireSharedInterruptibly(AbstractQueuedSynchronizer.java:1304)

at java.util.concurrent.Semaphore.acquire(Semaphore.java:312)

at com.rpc.demo.util.Foo.second(Foo.java:25)

at com.rpc.demo.util.Foo.main(Foo.java:45)


"VM Thread" os_prio=2 tid=0x000000005766f000 nid=0x8f8 runnable 


"GC task thread#0 (ParallelGC)" os_prio=0 tid=0x00000000024b0800 nid=0x1b8c runnable 


"GC task thread#1 (ParallelGC)" os_prio=0 tid=0x00000000024b2000 nid=0x1908 runnable 


"GC task thread#2 (ParallelGC)" os_prio=0 tid=0x00000000024b3800 nid=0xba8 runnable 


"GC task thread#3 (ParallelGC)" os_prio=0 tid=0x00000000024b5000 nid=0x2448 runnable 


"VM Periodic Task Thread" os_prio=2 tid=0x0000000059008000 nid=0x2abc waiting on condition 


JNI global references: 318


Heap

 PSYoungGen      total 36864K, used 5750K [0x00000000d7000000, 0x00000000d9900000, 0x0000000100000000)

  eden space 31744K, 18% used [0x00000000d7000000,0x00000000d759db48,0x00000000d8f00000)

  from space 5120K, 0% used [0x00000000d9400000,0x00000000d9400000,0x00000000d9900000)

  to   space 5120K, 0% used [0x00000000d8f00000,0x00000000d8f00000,0x00000000d9400000)

 ParOldGen       total 84992K, used 0K [0x0000000085000000, 0x000000008a300000, 0x00000000d7000000)

  object space 84992K, 0% used [0x0000000085000000,0x0000000085000000,0x000000008a300000)

 Metaspace       used 4395K, capacity 4722K, committed 4992K, reserved 1056768K

  class space    used 489K, capacity 535K, committed 640K, reserved 1048576K

104
Pack
Pack

Semaphore 是一种基于计数的信号量。它可以设定一个阈值,基于此,多个线程竞争获取许可信

号,做完自己的申请后归还,超过阈值后,线程申请许可信号将会被阻塞。


请先 登录 后评论