8042322: Enhance thread contexts in networking and nio
Reviewed-by: alanb, michaelm
--- a/jdk/src/java.base/share/classes/sun/misc/ManagedLocalsThread.java Thu Apr 02 11:43:11 2015 +0100
+++ b/jdk/src/java.base/share/classes/sun/misc/ManagedLocalsThread.java Thu Apr 02 11:43:11 2015 +0100
@@ -43,6 +43,11 @@
eraseThreadLocals();
}
+ public ManagedLocalsThread(String name) {
+ super(name);
+ eraseThreadLocals();
+ }
+
public ManagedLocalsThread(Runnable target, String name) {
super(target, name);
eraseThreadLocals();
--- a/jdk/src/java.base/share/classes/sun/net/NetworkServer.java Thu Apr 02 11:43:11 2015 +0100
+++ b/jdk/src/java.base/share/classes/sun/net/NetworkServer.java Thu Apr 02 11:43:11 2015 +0100
@@ -27,6 +27,7 @@
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 @@
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 @@
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();
}
--- a/jdk/src/java.base/share/classes/sun/net/www/MimeLauncher.java Thu Apr 02 11:43:11 2015 +0100
+++ b/jdk/src/java.base/share/classes/sun/net/www/MimeLauncher.java Thu Apr 02 11:43:11 2015 +0100
@@ -27,8 +27,9 @@
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;
--- a/jdk/src/java.base/share/classes/sun/net/www/http/KeepAliveCache.java Thu Apr 02 11:43:11 2015 +0100
+++ b/jdk/src/java.base/share/classes/sun/net/www/http/KeepAliveCache.java Thu Apr 02 11:43:11 2015 +0100
@@ -30,6 +30,7 @@
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 @@
java.security.AccessController.doPrivileged(
new java.security.PrivilegedAction<Void>() {
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
--- a/jdk/src/java.base/share/classes/sun/net/www/http/KeepAliveStream.java Thu Apr 02 11:43:11 2015 +0100
+++ b/jdk/src/java.base/share/classes/sun/net/www/http/KeepAliveStream.java Thu Apr 02 11:43:11 2015 +0100
@@ -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 @@
java.security.AccessController.doPrivileged(
new java.security.PrivilegedAction<Void>() {
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
--- a/jdk/src/java.base/share/classes/sun/nio/fs/AbstractPoller.java Thu Apr 02 11:43:11 2015 +0100
+++ b/jdk/src/java.base/share/classes/sun/nio/fs/AbstractPoller.java Thu Apr 02 11:43:11 2015 +0100
@@ -30,6 +30,7 @@
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 @@
AccessController.doPrivileged(new PrivilegedAction<Object>() {
@Override
public Object run() {
- Thread thr = new Thread(thisRunnable);
+ Thread thr = new ManagedLocalsThread(thisRunnable);
thr.setDaemon(true);
thr.start();
return null;
--- a/jdk/src/java.base/share/classes/sun/nio/fs/Cancellable.java Thu Apr 02 11:43:11 2015 +0100
+++ b/jdk/src/java.base/share/classes/sun/nio/fs/Cancellable.java Thu Apr 02 11:43:11 2015 +0100
@@ -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 @@
* 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()) {
--- a/jdk/src/java.base/share/classes/sun/nio/fs/PollingWatchService.java Thu Apr 02 11:43:11 2015 +0100
+++ b/jdk/src/java.base/share/classes/sun/nio/fs/PollingWatchService.java Thu Apr 02 11:43:11 2015 +0100
@@ -35,6 +35,7 @@
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 @@
.newSingleThreadScheduledExecutor(new ThreadFactory() {
@Override
public Thread newThread(Runnable r) {
- Thread t = new Thread(r);
+ Thread t = new ManagedLocalsThread(r);
t.setDaemon(true);
return t;
}});
--- a/jdk/src/java.base/windows/classes/sun/nio/ch/WindowsSelectorImpl.java Thu Apr 02 11:43:11 2015 +0100
+++ b/jdk/src/java.base/windows/classes/sun/nio/ch/WindowsSelectorImpl.java Thu Apr 02 11:43:11 2015 +0100
@@ -40,6 +40,7 @@
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 @@
}
// 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
--- a/jdk/src/jdk.httpserver/share/classes/sun/net/httpserver/ServerImpl.java Thu Apr 02 11:43:11 2015 +0100
+++ b/jdk/src/jdk.httpserver/share/classes/sun/net/httpserver/ServerImpl.java Thu Apr 02 11:43:11 2015 +0100
@@ -36,6 +36,7 @@
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 @@
if (executor == null) {
executor = new DefaultExecutor();
}
- dispatcherThread = new Thread (dispatcher);
+ dispatcherThread = new ManagedLocalsThread(dispatcher);
started = true;
dispatcherThread.start();
}