提交 5a607810 编写于 作者: C chegar

6882654: Remove dependency on java.util.concurrent from KeepAlive implementaion

Reviewed-by: michaelm
上级 010a7aac
...@@ -25,12 +25,11 @@ ...@@ -25,12 +25,11 @@
package sun.net.www.http; package sun.net.www.http;
import java.io.InputStream;
import java.io.IOException; import java.io.IOException;
import java.io.NotSerializableException; import java.io.NotSerializableException;
import java.util.*; import java.util.ArrayList;
import java.util.HashMap;
import java.net.URL; import java.net.URL;
import java.util.concurrent.ConcurrentHashMap;
/** /**
* A class that implements a cache of idle Http connections for keep-alive * A class that implements a cache of idle Http connections for keep-alive
...@@ -39,7 +38,7 @@ import java.util.concurrent.ConcurrentHashMap; ...@@ -39,7 +38,7 @@ import java.util.concurrent.ConcurrentHashMap;
* @author Dave Brown * @author Dave Brown
*/ */
public class KeepAliveCache public class KeepAliveCache
extends ConcurrentHashMap<KeepAliveKey, ClientVector> extends HashMap<KeepAliveKey, ClientVector>
implements Runnable { implements Runnable {
private static final long serialVersionUID = -2937172892064557949L; private static final long serialVersionUID = -2937172892064557949L;
...@@ -163,8 +162,8 @@ public class KeepAliveCache ...@@ -163,8 +162,8 @@ public class KeepAliveCache
* Errs on the side of caution (leave connections idle for a relatively * Errs on the side of caution (leave connections idle for a relatively
* short time). * short time).
*/ */
@Override
public void run() { public void run() {
int total_cache;
do { do {
try { try {
Thread.sleep(LIFETIME); Thread.sleep(LIFETIME);
...@@ -311,6 +310,7 @@ class KeepAliveKey { ...@@ -311,6 +310,7 @@ class KeepAliveKey {
/** /**
* Determine whether or not two objects of this type are equal * Determine whether or not two objects of this type are equal
*/ */
@Override
public boolean equals(Object obj) { public boolean equals(Object obj) {
if ((obj instanceof KeepAliveKey) == false) if ((obj instanceof KeepAliveKey) == false)
return false; return false;
...@@ -325,6 +325,7 @@ class KeepAliveKey { ...@@ -325,6 +325,7 @@ class KeepAliveKey {
* The hashCode() for this object is the string hashCode() of * The hashCode() for this object is the string hashCode() of
* concatenation of the protocol, host name and port. * concatenation of the protocol, host name and port.
*/ */
@Override
public int hashCode() { public int hashCode() {
String str = protocol+host+port; String str = protocol+host+port;
return this.obj == null? str.hashCode() : return this.obj == null? str.hashCode() :
......
...@@ -25,10 +25,7 @@ ...@@ -25,10 +25,7 @@
package sun.net.www.http; package sun.net.www.http;
import java.net.URL;
import java.net.HttpURLConnection;
import java.io.*; import java.io.*;
import java.util.StringTokenizer;
import sun.net.ProgressSource; import sun.net.ProgressSource;
import sun.net.www.MeteredStream; import sun.net.www.MeteredStream;
...@@ -50,9 +47,8 @@ class KeepAliveStream extends MeteredStream implements Hurryable { ...@@ -50,9 +47,8 @@ class KeepAliveStream extends MeteredStream implements Hurryable {
// has this KeepAliveStream been put on the queue for asynchronous cleanup. // has this KeepAliveStream been put on the queue for asynchronous cleanup.
protected boolean queuedForCleanup = false; protected boolean queuedForCleanup = false;
private static KeepAliveStreamCleaner queue = new KeepAliveStreamCleaner(); private static final KeepAliveStreamCleaner queue = new KeepAliveStreamCleaner();
private static Thread cleanerThread = null; private static Thread cleanerThread; // null
private static boolean startCleanupThread;
/** /**
* Constructor * Constructor
...@@ -155,43 +151,46 @@ class KeepAliveStream extends MeteredStream implements Hurryable { ...@@ -155,43 +151,46 @@ class KeepAliveStream extends MeteredStream implements Hurryable {
} }
} }
private static synchronized void queueForCleanup(KeepAliveCleanerEntry kace) { private static void queueForCleanup(KeepAliveCleanerEntry kace) {
if(queue != null && !kace.getQueuedForCleanup()) { synchronized(queue) {
if (!queue.offer(kace)) { if(!kace.getQueuedForCleanup()) {
kace.getHttpClient().closeServer(); if (!queue.offer(kace)) {
return; kace.getHttpClient().closeServer();
} return;
}
kace.setQueuedForCleanup(); kace.setQueuedForCleanup();
} queue.notifyAll();
}
startCleanupThread = (cleanerThread == null); boolean startCleanupThread = (cleanerThread == null);
if (!startCleanupThread) { if (!startCleanupThread) {
if (!cleanerThread.isAlive()) { if (!cleanerThread.isAlive()) {
startCleanupThread = true; startCleanupThread = true;
}
} }
}
if (startCleanupThread) { if (startCleanupThread) {
java.security.AccessController.doPrivileged( java.security.AccessController.doPrivileged(
new java.security.PrivilegedAction<Void>() { new java.security.PrivilegedAction<Void>() {
public Void run() { public Void run() {
// We want to create the Keep-Alive-SocketCleaner in the // We want to create the Keep-Alive-SocketCleaner in the
// system threadgroup // system threadgroup
ThreadGroup grp = Thread.currentThread().getThreadGroup(); ThreadGroup grp = Thread.currentThread().getThreadGroup();
ThreadGroup parent = null; ThreadGroup parent = null;
while ((parent = grp.getParent()) != null) { while ((parent = grp.getParent()) != null) {
grp = parent; grp = parent;
}
cleanerThread = new Thread(grp, queue, "Keep-Alive-SocketCleaner");
cleanerThread.setDaemon(true);
cleanerThread.setPriority(Thread.MAX_PRIORITY - 2);
cleanerThread.start();
return null;
} }
});
cleanerThread = new Thread(grp, queue, "Keep-Alive-SocketCleaner"); }
cleanerThread.setDaemon(true); } // queue
cleanerThread.setPriority(Thread.MAX_PRIORITY - 2);
cleanerThread.start();
return null;
}
});
}
} }
protected long remainingToRead() { protected long remainingToRead() {
......
...@@ -25,9 +25,8 @@ ...@@ -25,9 +25,8 @@
package sun.net.www.http; package sun.net.www.http;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.TimeUnit;
import java.io.IOException; import java.io.IOException;
import java.util.LinkedList;
import sun.net.NetProperties; import sun.net.NetProperties;
import java.security.AccessController; import java.security.AccessController;
import java.security.PrivilegedAction; import java.security.PrivilegedAction;
...@@ -44,7 +43,9 @@ import java.security.PrivilegedAction; ...@@ -44,7 +43,9 @@ import java.security.PrivilegedAction;
*/ */
@SuppressWarnings("serial") // never serialized @SuppressWarnings("serial") // never serialized
public class KeepAliveStreamCleaner extends LinkedBlockingQueue<KeepAliveCleanerEntry> implements Runnable class KeepAliveStreamCleaner
extends LinkedList<KeepAliveCleanerEntry>
implements Runnable
{ {
// maximum amount of remaining data that we will try to cleanup // maximum amount of remaining data that we will try to cleanup
protected static int MAX_DATA_REMAINING = 512; protected static int MAX_DATA_REMAINING = 512;
...@@ -78,23 +79,39 @@ public class KeepAliveStreamCleaner extends LinkedBlockingQueue<KeepAliveCleaner ...@@ -78,23 +79,39 @@ public class KeepAliveStreamCleaner extends LinkedBlockingQueue<KeepAliveCleaner
} }
public KeepAliveStreamCleaner() @Override
{ public boolean offer(KeepAliveCleanerEntry e) {
super(MAX_CAPACITY); if (size() >= MAX_CAPACITY)
} return false;
public KeepAliveStreamCleaner(int capacity) return super.offer(e);
{
super(capacity);
} }
@Override
public void run() public void run()
{ {
KeepAliveCleanerEntry kace = null; KeepAliveCleanerEntry kace = null;
do { do {
try { try {
kace = poll((long)TIMEOUT, TimeUnit.MILLISECONDS); synchronized(this) {
long before = System.currentTimeMillis();
long timeout = TIMEOUT;
while ((kace = poll()) == null) {
this.wait(timeout);
long after = System.currentTimeMillis();
long elapsed = after - before;
if (elapsed > timeout) {
/* one last try */
kace = poll();
break;
}
before = after;
timeout -= elapsed;
}
}
if(kace == null) if(kace == null)
break; break;
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册