mirror of
https://github.com/square/okhttp.git
synced 2026-01-25 16:01:38 +03:00
Use daemon threads in the connection pool.
This commit is contained in:
@@ -80,8 +80,9 @@ public class ConnectionPool {
|
||||
private final LinkedList<Connection> connections = new LinkedList<Connection>();
|
||||
|
||||
/** We use a single background thread to cleanup expired connections. */
|
||||
private final ExecutorService executorService =
|
||||
new ThreadPoolExecutor(0, 1, 60L, TimeUnit.SECONDS, new LinkedBlockingQueue<Runnable>());
|
||||
private final ExecutorService executorService = new ThreadPoolExecutor(0, 1,
|
||||
60L, TimeUnit.SECONDS, new LinkedBlockingQueue<Runnable>(),
|
||||
Util.daemonThreadFactory("OkHttp ConnectionPool"));
|
||||
private final Callable<Void> connectionsCleanupCallable = new Callable<Void>() {
|
||||
@Override public Void call() throws Exception {
|
||||
List<Connection> expiredConnections = new ArrayList<Connection>(MAX_CONNECTIONS_TO_CLEANUP);
|
||||
|
||||
@@ -20,10 +20,10 @@ package com.squareup.okhttp.internal;
|
||||
* Runnable implementation which always sets its thread name.
|
||||
*/
|
||||
public abstract class NamedRunnable implements Runnable {
|
||||
private String name;
|
||||
private final String name;
|
||||
|
||||
public NamedRunnable(String name) {
|
||||
this.name = name;
|
||||
public NamedRunnable(String format, Object... args) {
|
||||
this.name = String.format(format, args);
|
||||
}
|
||||
|
||||
@Override public final void run() {
|
||||
|
||||
@@ -32,6 +32,7 @@ import java.nio.charset.Charset;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.ThreadFactory;
|
||||
import java.util.concurrent.atomic.AtomicReference;
|
||||
|
||||
/** Junk drawer of utility methods. */
|
||||
@@ -332,4 +333,14 @@ public final class Util {
|
||||
public static <T> List<T> immutableList(List<T> list) {
|
||||
return Collections.unmodifiableList(new ArrayList<T>(list));
|
||||
}
|
||||
|
||||
public static ThreadFactory daemonThreadFactory(final String name) {
|
||||
return new ThreadFactory() {
|
||||
@Override public Thread newThread(Runnable runnable) {
|
||||
Thread result = new Thread(runnable, name);
|
||||
result.setDaemon(true);
|
||||
return result;
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
@@ -32,8 +32,6 @@ import java.util.concurrent.SynchronousQueue;
|
||||
import java.util.concurrent.ThreadPoolExecutor;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
import static java.util.concurrent.Executors.defaultThreadFactory;
|
||||
|
||||
/**
|
||||
* A socket connection to a remote peer. A connection hosts streams which can
|
||||
* send and receive data.
|
||||
@@ -77,9 +75,9 @@ public final class SpdyConnection implements Closeable {
|
||||
static final int GOAWAY_PROTOCOL_ERROR = 1;
|
||||
static final int GOAWAY_INTERNAL_ERROR = 2;
|
||||
|
||||
private static final ExecutorService executor =
|
||||
new ThreadPoolExecutor(0, Integer.MAX_VALUE, 60, TimeUnit.SECONDS,
|
||||
new SynchronousQueue<Runnable>(), defaultThreadFactory());
|
||||
private static final ExecutorService executor = new ThreadPoolExecutor(0,
|
||||
Integer.MAX_VALUE, 60, TimeUnit.SECONDS, new SynchronousQueue<Runnable>(),
|
||||
Util.daemonThreadFactory("OkHttp SpdyConnection"));
|
||||
|
||||
/** True if this peer initiated the connection. */
|
||||
final boolean client;
|
||||
@@ -202,15 +200,14 @@ public final class SpdyConnection implements Closeable {
|
||||
}
|
||||
|
||||
void writeSynResetLater(final int streamId, final int statusCode) {
|
||||
executor.submit(
|
||||
new NamedRunnable(String.format("Spdy Writer %s stream %d", hostName, streamId)) {
|
||||
@Override public void execute() {
|
||||
try {
|
||||
writeSynReset(streamId, statusCode);
|
||||
} catch (IOException ignored) {
|
||||
}
|
||||
}
|
||||
});
|
||||
executor.submit(new NamedRunnable("OkHttp SPDY Writer %s stream %d", hostName, streamId) {
|
||||
@Override public void execute() {
|
||||
try {
|
||||
writeSynReset(streamId, statusCode);
|
||||
} catch (IOException ignored) {
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
void writeSynReset(int streamId, int statusCode) throws IOException {
|
||||
@@ -218,15 +215,14 @@ public final class SpdyConnection implements Closeable {
|
||||
}
|
||||
|
||||
void writeWindowUpdateLater(final int streamId, final int deltaWindowSize) {
|
||||
executor.submit(
|
||||
new NamedRunnable(String.format("Spdy Writer %s stream %d", hostName, streamId)) {
|
||||
@Override public void execute() {
|
||||
try {
|
||||
writeWindowUpdate(streamId, deltaWindowSize);
|
||||
} catch (IOException ignored) {
|
||||
}
|
||||
}
|
||||
});
|
||||
executor.submit(new NamedRunnable("OkHttp SPDY Writer %s stream %d", hostName, streamId) {
|
||||
@Override public void execute() {
|
||||
try {
|
||||
writeWindowUpdate(streamId, deltaWindowSize);
|
||||
} catch (IOException ignored) {
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
void writeWindowUpdate(int streamId, int deltaWindowSize) throws IOException {
|
||||
@@ -254,7 +250,7 @@ public final class SpdyConnection implements Closeable {
|
||||
}
|
||||
|
||||
private void writePingLater(final int streamId, final Ping ping) {
|
||||
executor.submit(new NamedRunnable(String.format("Spdy Writer %s ping %d", hostName, streamId)) {
|
||||
executor.submit(new NamedRunnable("OkHttp SPDY Writer %s ping %d", hostName, streamId) {
|
||||
@Override public void execute() {
|
||||
try {
|
||||
writePing(streamId, ping);
|
||||
@@ -471,8 +467,7 @@ public final class SpdyConnection implements Closeable {
|
||||
return;
|
||||
}
|
||||
|
||||
executor.submit(
|
||||
new NamedRunnable(String.format("Callback %s stream %d", hostName, streamId)) {
|
||||
executor.submit(new NamedRunnable("OkHttp SPDY Callback %s stream %d", hostName, streamId) {
|
||||
@Override public void execute() {
|
||||
try {
|
||||
handler.receive(synStream);
|
||||
|
||||
Reference in New Issue
Block a user