This commit is contained in:
Lana Steuck 2016-02-11 16:06:02 -08:00
commit 11aecb99e1

View File

@ -28,62 +28,97 @@ package com.sun.xml.internal.ws.api.pipe;
import java.lang.reflect.Constructor; import java.lang.reflect.Constructor;
import java.security.AccessController; import java.security.AccessController;
import java.security.PrivilegedAction; import java.security.PrivilegedAction;
import java.util.concurrent.ThreadFactory;
/** /**
* Simple utility class to instantiate correct Thread instance * Simple utility class to instantiate correct Thread instance
* depending on runtime context (jdk/non-jdk usage) * depending on runtime context (jdk/non-jdk usage) and Java version.
* *
* @author miroslav.kos@oracle.com * @author miroslav.kos@oracle.com
*/ */
final class ThreadHelper { final class ThreadHelper {
private static final String SAFE_THREAD_NAME = "sun.misc.ManagedLocalsThread"; private static final String SAFE_THREAD_NAME = "sun.misc.ManagedLocalsThread";
private static final Constructor THREAD_CONSTRUCTOR;
private static final ThreadFactory threadFactory;
// no instantiating wanted // no instantiating wanted
private ThreadHelper() { private ThreadHelper() {
} }
static { static {
THREAD_CONSTRUCTOR = AccessController.doPrivileged( threadFactory = AccessController.doPrivileged(
new PrivilegedAction<Constructor> () { new PrivilegedAction<ThreadFactory> () {
@Override @Override
public Constructor run() { public ThreadFactory run() {
// In order of preference
try { try {
Class cls = Class.forName(SAFE_THREAD_NAME); try {
if (cls != null) { Class<Thread> cls = Thread.class;
return cls.getConstructor(Runnable.class); Constructor<Thread> ctr = cls.getConstructor(
ThreadGroup.class,
Runnable.class,
String.class,
long.class,
boolean.class);
return new JDK9ThreadFactory(ctr);
} catch (NoSuchMethodException ignored) {
// constructor newly added in Java SE 9
} }
Class<?> cls = Class.forName(SAFE_THREAD_NAME);
Constructor<?> ctr = cls.getConstructor(Runnable.class);
return new SunMiscThreadFactory(ctr);
} catch (ClassNotFoundException ignored) { } catch (ClassNotFoundException ignored) {
} catch (NoSuchMethodException ignored) { } catch (NoSuchMethodException ignored) {
} }
return null; return new LegacyThreadFactory();
} }
} }
); );
} }
static Thread createNewThread(final Runnable r) { static Thread createNewThread(final Runnable r) {
if (isJDKInternal()) { return threadFactory.newThread(r);
return AccessController.doPrivileged( }
new PrivilegedAction<Thread>() {
@Override // A Thread factory backed by the Thread constructor that
public Thread run() { // suppresses inheriting of inheritable thread-locals.
try { private static class JDK9ThreadFactory implements ThreadFactory {
return (Thread) THREAD_CONSTRUCTOR.newInstance(r); final Constructor<Thread> ctr;
} catch (Exception e) { JDK9ThreadFactory(Constructor<Thread> ctr) { this.ctr = ctr; }
return new Thread(r); @Override public Thread newThread(Runnable r) {
} try {
} return ctr.newInstance(null, r, "toBeReplaced", 0, false);
} } catch (ReflectiveOperationException x) {
); throw new InternalError(x);
} else { }
return new Thread(r);
} }
} }
private static boolean isJDKInternal() { // A Thread factory backed by sun.misc.ManagedLocalsThread
String className = ThreadHelper.class.getName(); private static class SunMiscThreadFactory implements ThreadFactory {
return className.contains(".internal."); final Constructor<?> ctr;
SunMiscThreadFactory(Constructor<?> ctr) { this.ctr = ctr; }
@Override public Thread newThread(Runnable r) {
return AccessController.doPrivileged(
new PrivilegedAction<Thread>() {
@Override
public Thread run() {
try {
return (Thread) ctr.newInstance(r);
} catch (Exception e) {
return new Thread(r);
}
}
}
);
}
}
// A Thread factory backed by new Thread(Runnable)
private static class LegacyThreadFactory implements ThreadFactory {
@Override public Thread newThread(Runnable r) {
return new Thread(r);
}
} }
} }