jstack命令案例分析:对wait的线程没有调用notify()导致程序无法停止

对wait的线程没有调用notify()导致的结果就是程序无法停止。
o.notify(); //必须,否则无法停止程序

public class T06_00_sync_wait_notify {
    public static void main(String[] args) {
        final Object o = new Object();

        char[] aI = "1234567".toCharArray();
        char[] aC = "ABCDEFG".toCharArray();

        new Thread(()->{
            synchronized (o) {
                for(char c : aI) {
                    System.out.print(c);
                    try {
                        o.notify();
                        o.wait(); //让出锁
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }

                // o.notify(); //必须,否则无法停止程序
            }

        }, "t1").start();

        new Thread(()->{
            synchronized (o) {
                for(char c : aC) {
                    System.out.print(c);
                    try {
                        o.notify();
                        o.wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }

                o.notify();
            }
        }, "t2").start();
    }
}

使用jstack进行分析:

2020-04-30 21:12:49
Full thread dump OpenJDK 64-Bit Server VM (11.0.5+10-post-Ubuntu-0ubuntu1.118.04 mixed mode, sharing):

Threads class SMR info:
_java_thread_list=0x00007f724c001ea0, length=11, elements={
0x00007f72900a3800, 0x00007f72900a5800, 0x00007f72900ab000, 0x00007f72900ad000,
0x00007f72900af000, 0x00007f72900b1000, 0x00007f72900e8800, 0x00007f72900ed800,
0x00007f72900f9800, 0x00007f7290014800, 0x00007f724c001000
}

"Reference Handler" #2 daemon prio=10 os_prio=0 cpu=0.15ms elapsed=70.83s tid=0x00007f72900a3800 nid=0x8ea waiting on condition  [0x00007f72783b3000]
   java.lang.Thread.State: RUNNABLE
	at java.lang.ref.Reference.waitForReferencePendingList(java.base@11.0.5/Native Method)
	at java.lang.ref.Reference.processPendingReferences(java.base@11.0.5/Reference.java:241)
	at java.lang.ref.Reference$ReferenceHandler.run(java.base@11.0.5/Reference.java:213)

"Finalizer" #3 daemon prio=8 os_prio=0 cpu=0.31ms elapsed=70.83s tid=0x00007f72900a5800 nid=0x8eb in Object.wait()  [0x00007f72782b2000]
   java.lang.Thread.State: WAITING (on object monitor)
	at java.lang.Object.wait(java.base@11.0.5/Native Method)
	- waiting on <0x00000000d43024f8> (a java.lang.ref.ReferenceQueue$Lock)
	at java.lang.ref.ReferenceQueue.remove(java.base@11.0.5/ReferenceQueue.java:155)
	- waiting to re-lock in wait() <0x00000000d43024f8> (a java.lang.ref.ReferenceQueue$Lock)
	at java.lang.ref.ReferenceQueue.remove(java.base@11.0.5/ReferenceQueue.java:176)
	at java.lang.ref.Finalizer$FinalizerThread.run(java.base@11.0.5/Finalizer.java:170)

"Signal Dispatcher" #4 daemon prio=9 os_prio=0 cpu=0.43ms elapsed=70.82s tid=0x00007f72900ab000 nid=0x8ec runnable  [0x0000000000000000]
   java.lang.Thread.State: RUNNABLE

"C2 CompilerThread0" #5 daemon prio=9 os_prio=0 cpu=10.71ms elapsed=70.82s tid=0x00007f72900ad000 nid=0x8ed waiting on condition  [0x0000000000000000]
   java.lang.Thread.State: RUNNABLE
   No compile task

"C1 CompilerThread0" #6 daemon prio=9 os_prio=0 cpu=24.92ms elapsed=70.82s tid=0x00007f72900af000 nid=0x8ee waiting on condition  [0x0000000000000000]
   java.lang.Thread.State: RUNNABLE
   No compile task

"Sweeper thread" #7 daemon prio=9 os_prio=0 cpu=0.53ms elapsed=70.81s tid=0x00007f72900b1000 nid=0x8ef runnable  [0x0000000000000000]
   java.lang.Thread.State: RUNNABLE

"Service Thread" #8 daemon prio=9 os_prio=0 cpu=0.08ms elapsed=70.77s tid=0x00007f72900e8800 nid=0x8f0 runnable  [0x0000000000000000]
   java.lang.Thread.State: RUNNABLE

"Common-Cleaner" #9 daemon prio=8 os_prio=0 cpu=0.27ms elapsed=70.76s tid=0x00007f72900ed800 nid=0x8f2 in Object.wait()  [0x00007f7268e64000]
   java.lang.Thread.State: TIMED_WAITING (on object monitor)
	at java.lang.Object.wait(java.base@11.0.5/Native Method)
	- waiting on <0x00000000d439b4c0> (a java.lang.ref.ReferenceQueue$Lock)
	at java.lang.ref.ReferenceQueue.remove(java.base@11.0.5/ReferenceQueue.java:155)
	- waiting to re-lock in wait() <0x00000000d439b4c0> (a java.lang.ref.ReferenceQueue$Lock)
	at jdk.internal.ref.CleanerImpl.run(java.base@11.0.5/CleanerImpl.java:148)
	at java.lang.Thread.run(java.base@11.0.5/Thread.java:834)
	at jdk.internal.misc.InnocuousThread.run(java.base@11.0.5/InnocuousThread.java:134)

"t2" #11 prio=5 os_prio=0 cpu=0.38ms elapsed=70.75s tid=0x00007f72900f9800 nid=0x8f4 in Object.wait()  [0x00007f7268c62000]
   java.lang.Thread.State: WAITING (on object monitor)
	at java.lang.Object.wait(java.base@11.0.5/Native Method)
	- waiting on <0x00000000d439d560> (a java.lang.Object)
	at java.lang.Object.wait(java.base@11.0.5/Object.java:328)
	at T06_00_sync_wait_notify.lambda$main$1(T06_00_sync_wait_notify.java:31)
	- waiting to re-lock in wait() <0x00000000d439d560> (a java.lang.Object)
	at T06_00_sync_wait_notify$$Lambda$2/0x0000000840060c40.run(Unknown Source)
	at java.lang.Thread.run(java.base@11.0.5/Thread.java:834)

"DestroyJavaVM" #12 prio=5 os_prio=0 cpu=69.22ms elapsed=70.75s tid=0x00007f7290014800 nid=0x8e3 waiting on condition  [0x0000000000000000]
   java.lang.Thread.State: RUNNABLE

"Attach Listener" #13 daemon prio=9 os_prio=0 cpu=0.73ms elapsed=44.86s tid=0x00007f724c001000 nid=0x92f waiting on condition  [0x0000000000000000]
   java.lang.Thread.State: RUNNABLE

"VM Thread" os_prio=0 cpu=3.33ms elapsed=70.84s tid=0x00007f72900a1000 nid=0x8e9 runnable  

"GC Thread#0" os_prio=0 cpu=0.39ms elapsed=70.86s tid=0x00007f729002d000 nid=0x8e4 runnable  

"G1 Main Marker" os_prio=0 cpu=0.38ms elapsed=70.85s tid=0x00007f7290043000 nid=0x8e5 runnable  

"G1 Conc#0" os_prio=0 cpu=0.04ms elapsed=70.85s tid=0x00007f7290044800 nid=0x8e6 runnable  

"G1 Refine#0" os_prio=0 cpu=0.38ms elapsed=70.85s tid=0x00007f7290074800 nid=0x8e7 runnable  

"G1 Young RemSet Sampling" os_prio=0 cpu=10.06ms elapsed=70.85s tid=0x00007f7290076000 nid=0x8e8 runnable  
"VM Periodic Task Thread" os_prio=0 cpu=48.15ms elapsed=70.77s tid=0x00007f72900eb000 nid=0x8f1 waiting on condition  

JNI global refs: 4, weak refs: 0


你可能感兴趣的:(JVM调优)