8042322: Enhance thread contexts in networking and nio

Reviewed-by: alanb, michaelm
This commit is contained in:
Chris Hegarty 2015-04-02 11:43:11 +01:00
parent b6eef64a98
commit 8747b64d35
10 changed files with 25 additions and 26 deletions

View File

@ -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();

View File

@ -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();
}

View File

@ -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;

View File

@ -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<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

View File

@ -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<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

View File

@ -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<Object>() {
@Override
public Object run() {
Thread thr = new Thread(thisRunnable);
Thread thr = new ManagedLocalsThread(thisRunnable);
thr.setDaemon(true);
thr.start();
return null;

View File

@ -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()) {

View File

@ -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;
}});

View File

@ -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

View File

@ -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();
}