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

CompletableFuture.ThreadPerTaskExecutor does not throw NPE in #execute

XMLWordPrintable

    • b05
    • generic
    • generic
    • Verified

      ADDITIONAL SYSTEM INFORMATION :
      Mac OS X 10.14.6
      OpenJDK 16-ea (build 16-ea+20-1092), AdoptOpenJDK 15 (build 15+36), AdoptOpenJDK 11 (build 11.0.8+10)

      A DESCRIPTION OF THE PROBLEM :
      Executor#execute is documented throw NullPointerException if the passed Runnable is null. The Executor implementations in the JDK conform to this behaviour with the exception of ThreadPerTaskExecutor.

      The implementation of ThreadPerTaskExecutor is to call Thread#Thread(Runnable) and start the thread. Such constructor does not perform a null-check on the Runnable. Rather, if the Runnable is null, the Thread will perform a no-op in Thread#run.

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      1. Ensure the parallelism of the common ForkJoinPool is 1
      2. Obtain the default Executor of CompletableFuture
      3. Call Executor.execute with a null Runnable

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      A NullPointerException is thrown in accordance with Executor.execute.
      ACTUAL -
      No NPE is thrown by ThreadPerTaskExecutor.

      ---------- BEGIN SOURCE ----------
      package completablefuture.threadpertaskexecutor;

      import java.util.concurrent.CompletableFuture;
      import java.util.concurrent.Executor;
      import java.util.concurrent.ForkJoinPool;

      public class CompletableFutureMain {

      public static void main(String[] args) {
      System.setProperty("java.util.concurrent.ForkJoinPool.common.parallelism", Integer.toString(1));
      System.out.println("Common pool parallelism: " + ForkJoinPool.getCommonPoolParallelism());

      Executor executor = new CompletableFuture<>().defaultExecutor();
      executor.execute(null);
      System.out.println("Expected NPE but got no-op");
      }

      }
      ---------- END SOURCE ----------

      CUSTOMER SUBMITTED WORKAROUND :
      The calling code must explicitly null-check Runnables as a workaround.

      FREQUENCY : always


            martin Martin Buchholz
            webbuggrp Webbug Group
            Votes:
            0 Vote for this issue
            Watchers:
            6 Start watching this issue

              Created:
              Updated:
              Resolved: