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

ContextClassLoader cannot be reset on threads in ForkJoinPool.commonPool()

XMLWordPrintable

      A DESCRIPTION OF THE PROBLEM :
      Once the ContextClassLoader is changed on a thread in ForkJoinPool.commonPool() to a custom ClassLoader, it cannot be manually reset to the original value (ClassLoader.getSystemClassLoader()).

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      Compile and start the supplied java program.
      The program executes a task by using the ForkJoinPool.commonPool() which first changes the ContextClassLoader to a custom ClassLoader and then changes it back to the original ClassLoader.

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      The ContextClassCloader can be reset to its original value.
      ACTUAL -
      The ContextClassLoader remains the custom ClassLoader.

      ---------- BEGIN SOURCE ----------
      public class ContextClassLoaderInForkJoinPoolCommonPool {
          public static void main(String[] args) throws InterruptedException, ExecutionException {

              CountDownLatch latch = new CountDownLatch(1);

              ForkJoinPool.commonPool().execute(() -> {
                  Thread thread = Thread.currentThread();

                  ClassLoader originalCCL = thread.getContextClassLoader();

                  // Set a custom ContextClassLoader and verify that it has been set
                  ClassLoader customCCL = new URLClassLoader(new URL[0]);
                  thread.setContextClassLoader(customCCL);
                  if (thread.getContextClassLoader() != customCCL) {
                      throw new RuntimeException("ERROR: cannot set custom ContextClassLoader");
                  }

                  // Restore the original ContextClassLoader and verify that it has been set
                  thread.setContextClassLoader(originalCCL);
                  if (thread.getContextClassLoader() != originalCCL) {
                      throw new RuntimeException("ERROR: cannot reset to original ContextClassLoader");
                  }

                  latch.countDown();
              });

              latch.await();
          }
      }
      ---------- END SOURCE ----------

            jjose Johny Jose
            webbuggrp Webbug Group
            Votes:
            0 Vote for this issue
            Watchers:
            6 Start watching this issue

              Created:
              Updated: