From 8747b64d35cdbe779f552e949ba4feb3f953916d Mon Sep 17 00:00:00 2001 From: Chris Hegarty Date: Thu, 2 Apr 2015 11:43:11 +0100 Subject: [PATCH] 8042322: Enhance thread contexts in networking and nio Reviewed-by: alanb, michaelm --- .../share/classes/sun/misc/ManagedLocalsThread.java | 5 +++++ .../share/classes/sun/net/NetworkServer.java | 5 +++-- .../share/classes/sun/net/www/MimeLauncher.java | 3 ++- .../classes/sun/net/www/http/KeepAliveCache.java | 11 ++--------- .../classes/sun/net/www/http/KeepAliveStream.java | 12 +++--------- .../share/classes/sun/nio/fs/AbstractPoller.java | 3 ++- .../share/classes/sun/nio/fs/Cancellable.java | 3 ++- .../classes/sun/nio/fs/PollingWatchService.java | 3 ++- .../classes/sun/nio/ch/WindowsSelectorImpl.java | 3 ++- .../share/classes/sun/net/httpserver/ServerImpl.java | 3 ++- 10 files changed, 25 insertions(+), 26 deletions(-) diff --git a/jdk/src/java.base/share/classes/sun/misc/ManagedLocalsThread.java b/jdk/src/java.base/share/classes/sun/misc/ManagedLocalsThread.java index f7044124180..cd75ef237ce 100644 --- a/jdk/src/java.base/share/classes/sun/misc/ManagedLocalsThread.java +++ b/jdk/src/java.base/share/classes/sun/misc/ManagedLocalsThread.java @@ -43,6 +43,11 @@ public class ManagedLocalsThread extends Thread { eraseThreadLocals(); } + public ManagedLocalsThread(String name) { + super(name); + eraseThreadLocals(); + } + public ManagedLocalsThread(Runnable target, String name) { super(target, name); eraseThreadLocals(); diff --git a/jdk/src/java.base/share/classes/sun/net/NetworkServer.java b/jdk/src/java.base/share/classes/sun/net/NetworkServer.java index d4a9cc78e5b..6937775d40e 100644 --- a/jdk/src/java.base/share/classes/sun/net/NetworkServer.java +++ b/jdk/src/java.base/share/classes/sun/net/NetworkServer.java @@ -27,6 +27,7 @@ package sun.net; import java.io.*; import java.net.Socket; import java.net.ServerSocket; +import sun.misc.ManagedLocalsThread; /** * This is the base class for network servers. To define a new type @@ -72,7 +73,7 @@ public class NetworkServer implements Runnable, Cloneable { NetworkServer n = (NetworkServer)clone(); n.serverSocket = null; n.clientSocket = ns; - new Thread(n).start(); + new ManagedLocalsThread(n).start(); } catch(Exception e) { System.out.print("Server failure\n"); e.printStackTrace(); @@ -107,7 +108,7 @@ public class NetworkServer implements Runnable, Cloneable { for each new connection. */ final public void startServer(int port) throws IOException { serverSocket = new ServerSocket(port, 50); - serverInstance = new Thread(this); + serverInstance = new ManagedLocalsThread(this); serverInstance.start(); } diff --git a/jdk/src/java.base/share/classes/sun/net/www/MimeLauncher.java b/jdk/src/java.base/share/classes/sun/net/www/MimeLauncher.java index ee4fb40eeca..32b3603de82 100644 --- a/jdk/src/java.base/share/classes/sun/net/www/MimeLauncher.java +++ b/jdk/src/java.base/share/classes/sun/net/www/MimeLauncher.java @@ -27,8 +27,9 @@ package sun.net.www; import java.net.URL; import java.io.*; import java.util.StringTokenizer; +import sun.misc.ManagedLocalsThread; -class MimeLauncher extends Thread { +class MimeLauncher extends ManagedLocalsThread { java.net.URLConnection uc; MimeEntry m; String genericTempFileTemplate; diff --git a/jdk/src/java.base/share/classes/sun/net/www/http/KeepAliveCache.java b/jdk/src/java.base/share/classes/sun/net/www/http/KeepAliveCache.java index 16f02d7cdde..1b3d4295bf2 100644 --- a/jdk/src/java.base/share/classes/sun/net/www/http/KeepAliveCache.java +++ b/jdk/src/java.base/share/classes/sun/net/www/http/KeepAliveCache.java @@ -30,6 +30,7 @@ import java.io.NotSerializableException; import java.util.ArrayList; import java.util.HashMap; import java.net.URL; +import sun.misc.InnocuousThread; /** * A class that implements a cache of idle Http connections for keep-alive @@ -95,15 +96,7 @@ public class KeepAliveCache java.security.AccessController.doPrivileged( new java.security.PrivilegedAction() { public Void run() { - // We want to create the Keep-Alive-Timer in the - // system threadgroup - ThreadGroup grp = Thread.currentThread().getThreadGroup(); - ThreadGroup parent = null; - while ((parent = grp.getParent()) != null) { - grp = parent; - } - - keepAliveTimer = new Thread(grp, cache, "Keep-Alive-Timer"); + keepAliveTimer = new InnocuousThread(cache, "Keep-Alive-Timer"); keepAliveTimer.setDaemon(true); keepAliveTimer.setPriority(Thread.MAX_PRIORITY - 2); // Set the context class loader to null in order to avoid diff --git a/jdk/src/java.base/share/classes/sun/net/www/http/KeepAliveStream.java b/jdk/src/java.base/share/classes/sun/net/www/http/KeepAliveStream.java index 9155c4ae8c5..53835df7f6a 100644 --- a/jdk/src/java.base/share/classes/sun/net/www/http/KeepAliveStream.java +++ b/jdk/src/java.base/share/classes/sun/net/www/http/KeepAliveStream.java @@ -26,6 +26,8 @@ package sun.net.www.http; import java.io.*; + +import sun.misc.InnocuousThread; import sun.net.ProgressSource; import sun.net.www.MeteredStream; @@ -171,15 +173,7 @@ class KeepAliveStream extends MeteredStream implements Hurryable { java.security.AccessController.doPrivileged( new java.security.PrivilegedAction() { public Void run() { - // We want to create the Keep-Alive-SocketCleaner in the - // system threadgroup - ThreadGroup grp = Thread.currentThread().getThreadGroup(); - ThreadGroup parent = null; - while ((parent = grp.getParent()) != null) { - grp = parent; - } - - cleanerThread = new Thread(grp, queue, "Keep-Alive-SocketCleaner"); + cleanerThread = new InnocuousThread(queue, "Keep-Alive-SocketCleaner"); cleanerThread.setDaemon(true); cleanerThread.setPriority(Thread.MAX_PRIORITY - 2); // Set the context class loader to null in order to avoid diff --git a/jdk/src/java.base/share/classes/sun/nio/fs/AbstractPoller.java b/jdk/src/java.base/share/classes/sun/nio/fs/AbstractPoller.java index 34d4ea4e3bc..8dd078db0bc 100644 --- a/jdk/src/java.base/share/classes/sun/nio/fs/AbstractPoller.java +++ b/jdk/src/java.base/share/classes/sun/nio/fs/AbstractPoller.java @@ -30,6 +30,7 @@ import java.security.AccessController; import java.security.PrivilegedAction; import java.io.IOException; import java.util.*; +import sun.misc.ManagedLocalsThread; /** * Base implementation of background poller thread used in watch service @@ -59,7 +60,7 @@ abstract class AbstractPoller implements Runnable { AccessController.doPrivileged(new PrivilegedAction() { @Override public Object run() { - Thread thr = new Thread(thisRunnable); + Thread thr = new ManagedLocalsThread(thisRunnable); thr.setDaemon(true); thr.start(); return null; diff --git a/jdk/src/java.base/share/classes/sun/nio/fs/Cancellable.java b/jdk/src/java.base/share/classes/sun/nio/fs/Cancellable.java index 0aa6521f19b..648d4089ce8 100644 --- a/jdk/src/java.base/share/classes/sun/nio/fs/Cancellable.java +++ b/jdk/src/java.base/share/classes/sun/nio/fs/Cancellable.java @@ -25,6 +25,7 @@ package sun.nio.fs; +import sun.misc.ManagedLocalsThread; import sun.misc.Unsafe; import java.util.concurrent.ExecutionException; @@ -117,7 +118,7 @@ abstract class Cancellable implements Runnable { * thread by writing into the memory location that it polls cooperatively. */ static void runInterruptibly(Cancellable task) throws ExecutionException { - Thread t = new Thread(task); + Thread t = new ManagedLocalsThread(task); t.start(); boolean cancelledByInterrupt = false; while (t.isAlive()) { diff --git a/jdk/src/java.base/share/classes/sun/nio/fs/PollingWatchService.java b/jdk/src/java.base/share/classes/sun/nio/fs/PollingWatchService.java index d3d9891223d..bc4af73e762 100644 --- a/jdk/src/java.base/share/classes/sun/nio/fs/PollingWatchService.java +++ b/jdk/src/java.base/share/classes/sun/nio/fs/PollingWatchService.java @@ -35,6 +35,7 @@ import java.io.IOException; import java.util.*; import java.util.concurrent.*; import com.sun.nio.file.SensitivityWatchEventModifier; +import sun.misc.ManagedLocalsThread; /** * Simple WatchService implementation that uses periodic tasks to poll @@ -58,7 +59,7 @@ class PollingWatchService .newSingleThreadScheduledExecutor(new ThreadFactory() { @Override public Thread newThread(Runnable r) { - Thread t = new Thread(r); + Thread t = new ManagedLocalsThread(r); t.setDaemon(true); return t; }}); diff --git a/jdk/src/java.base/windows/classes/sun/nio/ch/WindowsSelectorImpl.java b/jdk/src/java.base/windows/classes/sun/nio/ch/WindowsSelectorImpl.java index eec35ea9518..28fc83a0b31 100644 --- a/jdk/src/java.base/windows/classes/sun/nio/ch/WindowsSelectorImpl.java +++ b/jdk/src/java.base/windows/classes/sun/nio/ch/WindowsSelectorImpl.java @@ -40,6 +40,7 @@ import java.util.List; import java.util.ArrayList; import java.util.HashMap; import java.util.Iterator; +import sun.misc.ManagedLocalsThread; /** * A multi-threaded implementation of Selector for Windows. @@ -403,7 +404,7 @@ final class WindowsSelectorImpl extends SelectorImpl { } // Represents a helper thread used for select. - private final class SelectThread extends Thread { + private final class SelectThread extends ManagedLocalsThread { private final int index; // index of this thread final SubSelector subSelector; private long lastRun = 0; // last run number diff --git a/jdk/src/jdk.httpserver/share/classes/sun/net/httpserver/ServerImpl.java b/jdk/src/jdk.httpserver/share/classes/sun/net/httpserver/ServerImpl.java index 24ab8b014db..5b97c16477c 100644 --- a/jdk/src/jdk.httpserver/share/classes/sun/net/httpserver/ServerImpl.java +++ b/jdk/src/jdk.httpserver/share/classes/sun/net/httpserver/ServerImpl.java @@ -36,6 +36,7 @@ import javax.net.ssl.*; import com.sun.net.httpserver.*; import java.security.AccessController; import java.security.PrivilegedAction; +import sun.misc.ManagedLocalsThread; import sun.net.httpserver.HttpConnection.State; /** @@ -142,7 +143,7 @@ class ServerImpl implements TimeSource { if (executor == null) { executor = new DefaultExecutor(); } - dispatcherThread = new Thread (dispatcher); + dispatcherThread = new ManagedLocalsThread(dispatcher); started = true; dispatcherThread.start(); }