Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8213361

Application hangs when lambda task submitted to executor from <clinit>

XMLWordPrintable

      A DESCRIPTION OF THE PROBLEM :
      If we submit a Runnable or Callable task, implemented by a lambda function, to a ThreadPoolExecutor, from a static initializer block, then the executor's thread will hang, the task won't be executed. Also reproduced on 8u171.

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      Execute the attached source code. It will hang. If I move the Executor::submit call into the main() method, it works as intended and prints the "q" character. But if the code is in the <clinit> block, it does nothing and waits forever.

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      "q" character printed.
      ACTUAL -
      Nothing happens, main threads waits for the executor thread, and the executor waits on some int[] monitor according to NetBeans debugger.

      jcmd Thread.print result:

      2018-11-04 16:34:24
      Full thread dump Java HotSpot(TM) 64-Bit Server VM (25.171-b11 mixed mode):

      "pool-1-thread-1" #14 prio=5 os_prio=0 tid=0x0000000024925800 nid=0x449c in Object.wait() [0x00000000251be000]
         java.lang.Thread.State: RUNNABLE
              at Main$$Lambda$1/26117480.call(Unknown Source)
              at java.util.concurrent.FutureTask.run(FutureTask.java:266)
              at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
              at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
              at java.lang.Thread.run(Thread.java:748)

      "Service Thread" #13 daemon prio=9 os_prio=0 tid=0x0000000023810000 nid=0x4370 runnable [0x0000000000000000]
         java.lang.Thread.State: RUNNABLE

      "C1 CompilerThread3" #12 daemon prio=9 os_prio=2 tid=0x0000000023800800 nid=0x24f4 waiting on condition [0x0000000000000000]
         java.lang.Thread.State: RUNNABLE

      "C2 CompilerThread2" #11 daemon prio=9 os_prio=2 tid=0x0000000023800000 nid=0x3b84 waiting on condition [0x0000000000000000]
         java.lang.Thread.State: RUNNABLE

      "C2 CompilerThread1" #10 daemon prio=9 os_prio=2 tid=0x00000000237fe000 nid=0x41c4 waiting on condition [0x0000000000000000]
         java.lang.Thread.State: RUNNABLE

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

      "JDWP Command Reader" #8 daemon prio=10 os_prio=0 tid=0x00000000237e7800 nid=0x1da0 runnable [0x0000000000000000]
         java.lang.Thread.State: RUNNABLE

      "JDWP Event Helper Thread" #7 daemon prio=10 os_prio=0 tid=0x00000000237de000 nid=0x2548 runnable [0x0000000000000000]
         java.lang.Thread.State: RUNNABLE

      "JDWP Transport Listener: dt_shmem" #6 daemon prio=10 os_prio=0 tid=0x00000000237d2800 nid=0x2bd0 runnable [0x0000000000000000]
         java.lang.Thread.State: RUNNABLE

      "Attach Listener" #5 daemon prio=5 os_prio=2 tid=0x00000000237c1000 nid=0x2818 waiting on condition [0x0000000000000000]
         java.lang.Thread.State: RUNNABLE

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

      "Finalizer" #3 daemon prio=8 os_prio=1 tid=0x00000000237b0800 nid=0x3298 in Object.wait() [0x0000000023c8e000]
         java.lang.Thread.State: WAITING (on object monitor)
              at java.lang.Object.wait(Native Method)
              - waiting on <0x0000000740708ed0> (a java.lang.ref.ReferenceQueue$Lock)
              at java.lang.ref.ReferenceQueue.remove(ReferenceQueue.java:143)
              - locked <0x0000000740708ed0> (a java.lang.ref.ReferenceQueue$Lock)
              at java.lang.ref.ReferenceQueue.remove(ReferenceQueue.java:164)
              at java.lang.ref.Finalizer$FinalizerThread.run(Finalizer.java:212)

      "Reference Handler" #2 daemon prio=10 os_prio=2 tid=0x0000000003329000 nid=0x3488 in Object.wait() [0x000000002378e000]
         java.lang.Thread.State: WAITING (on object monitor)
              at java.lang.Object.wait(Native Method)
              - waiting on <0x0000000740706bf8> (a java.lang.ref.Reference$Lock)
              at java.lang.Object.wait(Object.java:502)
              at java.lang.ref.Reference.tryHandlePending(Reference.java:191)
              - locked <0x0000000740706bf8> (a java.lang.ref.Reference$Lock)
              at java.lang.ref.Reference$ReferenceHandler.run(Reference.java:153)

      "main" #1 prio=5 os_prio=0 tid=0x0000000003232800 nid=0x164 waiting on condition [0x000000000322e000]
         java.lang.Thread.State: WAITING (parking)
              at sun.misc.Unsafe.park(Native Method)
              - parking to wait for <0x00000007408c3b40> (a java.util.concurrent.FutureTask)
              at java.util.concurrent.locks.LockSupport.park(LockSupport.java:175)
              at java.util.concurrent.FutureTask.awaitDone(FutureTask.java:429)
              at java.util.concurrent.FutureTask.get(FutureTask.java:191)
              at Main.<clinit>(Main.java:8)

      "VM Thread" os_prio=2 tid=0x00000000218ca000 nid=0x41f0 runnable

      "GC task thread#0 (ParallelGC)" os_prio=0 tid=0x0000000003248000 nid=0x3b88 runnable

      "GC task thread#1 (ParallelGC)" os_prio=0 tid=0x0000000003249800 nid=0x270c runnable

      "GC task thread#2 (ParallelGC)" os_prio=0 tid=0x000000000324b000 nid=0x1c74 runnable

      "GC task thread#3 (ParallelGC)" os_prio=0 tid=0x000000000324d000 nid=0x2d5c runnable

      "GC task thread#4 (ParallelGC)" os_prio=0 tid=0x0000000003250000 nid=0x3aa0 runnable

      "GC task thread#5 (ParallelGC)" os_prio=0 tid=0x0000000003251000 nid=0x3868 runnable

      "GC task thread#6 (ParallelGC)" os_prio=0 tid=0x0000000003254800 nid=0x18c0 runnable

      "GC task thread#7 (ParallelGC)" os_prio=0 tid=0x0000000003255800 nid=0x470 runnable

      "VM Periodic Task Thread" os_prio=2 tid=0x0000000023812800 nid=0x281c waiting on condition

      JNI global references: 1568

      ---------- BEGIN SOURCE ----------

      import java.util.concurrent.Executors;

      public class Main {

          static {
              try {
                  System.out.println(Executors.newSingleThreadExecutor().submit(() -> "q").get());
              } catch (Exception ex) {
                  ex.printStackTrace();
              }
          }

          public static void main(String[] args) {
          }

      }

      ---------- END SOURCE ----------

      FREQUENCY : always


            dholmes David Holmes
            webbuggrp Webbug Group
            Votes:
            0 Vote for this issue
            Watchers:
            6 Start watching this issue

              Created:
              Updated:
              Resolved: