Skip to content

Commit

Permalink
Issue #5859 Changes after review
Browse files Browse the repository at this point in the history
Signed-off-by: Jan Bartel <janb@webtide.com>
  • Loading branch information
janbartel committed Jan 21, 2021
1 parent 4a93a00 commit a7847d3
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 11 deletions.
Expand Up @@ -20,9 +20,10 @@

import java.security.AccessController;
import java.security.PrivilegedAction;
import java.util.function.Supplier;

/**
* ThreadCreator
* PrivilegedThreadFactory
*
* Convenience class to ensure that a new Thread is created
* inside a privileged block. This prevents the Thread constructor
Expand All @@ -32,21 +33,23 @@
* reference the context classloader - and remembers it for the
* lifetime of the Thread.
*/
class ThreadCreator
class PrivilegedThreadFactory
{
interface Factory<T extends Thread>
{
T newThread();
}

static <T extends Thread> T create(Factory<T> maker)
/**
* Use a Supplier to make a new thread, calling it within
* a privileged block to prevent classloader pinning.
*
* @param newThreadSupplier a Supplier to create a fresh thread
* @return a new thread, protected from classloader pinning.
*/
static <T extends Thread> T newThread(Supplier<T> newThreadSupplier)
{
return AccessController.doPrivileged(new PrivilegedAction<T>()
{
@Override
public T run()
{
return maker.newThread();
return newThreadSupplier.get();
}
});
}
Expand Down
Expand Up @@ -687,7 +687,7 @@ private boolean addCounts(int deltaThreads, int deltaIdle)
@Override
public Thread newThread(Runnable runnable)
{
return ThreadCreator.create(() ->
return PrivilegedThreadFactory.newThread(() ->
{
Thread thread = new Thread(_threadGroup, runnable);
thread.setDaemon(isDaemon());
Expand Down
Expand Up @@ -38,7 +38,7 @@
public class ShutdownThread extends Thread
{
private static final Logger LOG = Log.getLogger(ShutdownThread.class);
private static final ShutdownThread _thread = ThreadCreator.create(() ->
private static final ShutdownThread _thread = PrivilegedThreadFactory.newThread(() ->
{
return new ShutdownThread();
});
Expand Down

0 comments on commit a7847d3

Please sign in to comment.