From 596444f958d3a6eab2883fb177ad74da15ff993a Mon Sep 17 00:00:00 2001 From: Jan Bartel Date: Mon, 18 Jan 2021 18:46:03 +0100 Subject: [PATCH] Issue #5859 Post review changes Signed-off-by: Jan Bartel --- .../jetty/util/thread/QueuedThreadPool.java | 2 +- .../util/thread/QueuedThreadPoolTest.java | 18 +++++++++++++++--- 2 files changed, 16 insertions(+), 4 deletions(-) diff --git a/jetty-util/src/main/java/org/eclipse/jetty/util/thread/QueuedThreadPool.java b/jetty-util/src/main/java/org/eclipse/jetty/util/thread/QueuedThreadPool.java index 47ceda7891cb..82b71036048e 100644 --- a/jetty-util/src/main/java/org/eclipse/jetty/util/thread/QueuedThreadPool.java +++ b/jetty-util/src/main/java/org/eclipse/jetty/util/thread/QueuedThreadPool.java @@ -689,7 +689,7 @@ public Thread newThread(Runnable runnable) thread.setDaemon(isDaemon()); thread.setPriority(getThreadsPriority()); thread.setName(_name + "-" + thread.getId()); - thread.setContextClassLoader(QueuedThreadPool.class.getClassLoader()); + thread.setContextClassLoader(this.getClass().getClassLoader()); return thread; } diff --git a/jetty-util/src/test/java/org/eclipse/jetty/util/thread/QueuedThreadPoolTest.java b/jetty-util/src/test/java/org/eclipse/jetty/util/thread/QueuedThreadPoolTest.java index bdc8430aeea7..f84c512fadb0 100644 --- a/jetty-util/src/test/java/org/eclipse/jetty/util/thread/QueuedThreadPoolTest.java +++ b/jetty-util/src/test/java/org/eclipse/jetty/util/thread/QueuedThreadPoolTest.java @@ -19,6 +19,8 @@ package org.eclipse.jetty.util.thread; import java.io.Closeable; +import java.net.URL; +import java.net.URLClassLoader; import java.util.concurrent.CountDownLatch; import java.util.concurrent.TimeUnit; import java.util.concurrent.atomic.AtomicInteger; @@ -839,16 +841,26 @@ public void testContextClassLoader() throws Exception { QueuedThreadPool tp = new QueuedThreadPool(); tp.setMinThreads(1); - tp.setMaxThreads(2); + tp.setMaxThreads(3); tp.setIdleTimeout(1000); tp.setThreadsPriority(Thread.NORM_PRIORITY - 1); try (StacklessLogging stackless = new StacklessLogging(QueuedThreadPool.class)) { - tp.start(); - tp.execute(() -> + //change the current thread's classloader to something else + Thread.currentThread().setContextClassLoader(new URLClassLoader(new URL[] {})); + + //create a new thread + Thread t = tp.newThread(() -> { + //the executing thread should be still set to the classloader of the QueuedThreadPool, + //not that of the thread that created this thread. assertThat(Thread.currentThread().getContextClassLoader(), Matchers.equalTo(QueuedThreadPool.class.getClassLoader())); }); + + //new thread should be set to the classloader of the QueuedThreadPool + assertThat(t.getContextClassLoader(), Matchers.equalTo(QueuedThreadPool.class.getClassLoader())); + + t.start(); } }