From 28216aa971ea65938117107542152abd532a5384 Mon Sep 17 00:00:00 2001 From: Viktor Klang Date: Thu, 4 Apr 2024 18:32:59 +0000 Subject: [PATCH] 8328366: Thread.setContextClassloader from thread in FJP commonPool task no longer works after JDK-8327501 Reviewed-by: mchung, alanb --- .../share/classes/java/util/concurrent/ForkJoinPool.java | 7 +------ test/jdk/java/util/concurrent/tck/ForkJoinPool9Test.java | 8 ++++++++ 2 files changed, 9 insertions(+), 6 deletions(-) diff --git a/src/java.base/share/classes/java/util/concurrent/ForkJoinPool.java b/src/java.base/share/classes/java/util/concurrent/ForkJoinPool.java index 98e3cd2917b..f64ba4bf998 100644 --- a/src/java.base/share/classes/java/util/concurrent/ForkJoinPool.java +++ b/src/java.base/share/classes/java/util/concurrent/ForkJoinPool.java @@ -1140,12 +1140,7 @@ public class ForkJoinPool extends AbstractExecutorService { boolean isCommon = (pool.workerNamePrefix == null); @SuppressWarnings("removal") SecurityManager sm = System.getSecurityManager(); - if (sm == null) { - if (isCommon) - return new ForkJoinWorkerThread.InnocuousForkJoinWorkerThread(pool); - else - return new ForkJoinWorkerThread(null, pool, true, false); - } else if (isCommon) + if (sm != null && isCommon) return newCommonWithACC(pool); else return newRegularWithACC(pool); diff --git a/test/jdk/java/util/concurrent/tck/ForkJoinPool9Test.java b/test/jdk/java/util/concurrent/tck/ForkJoinPool9Test.java index 266c2a036fe..a87aa7b916b 100644 --- a/test/jdk/java/util/concurrent/tck/ForkJoinPool9Test.java +++ b/test/jdk/java/util/concurrent/tck/ForkJoinPool9Test.java @@ -79,6 +79,9 @@ public class ForkJoinPool9Test extends JSR166TestCase { assertSame(ForkJoinPool.commonPool(), ForkJoinTask.getPool()); Thread currentThread = Thread.currentThread(); + ClassLoader preexistingContextClassLoader = + currentThread.getContextClassLoader(); + Stream.of(systemClassLoader, null).forEach(cl -> { if (randomBoolean()) // should always be permitted, without effect @@ -95,6 +98,11 @@ public class ForkJoinPool9Test extends JSR166TestCase { () -> System.getProperty("foo"), () -> currentThread.setContextClassLoader( classLoaderDistinctFromSystemClassLoader)); + else { + currentThread.setContextClassLoader(classLoaderDistinctFromSystemClassLoader); + assertSame(currentThread.getContextClassLoader(), classLoaderDistinctFromSystemClassLoader); + currentThread.setContextClassLoader(preexistingContextClassLoader); + } // TODO ? // if (haveSecurityManager // && Thread.currentThread().getClass().getSimpleName()