Bring back PeerGroupThreadFactory

This commit is contained in:
Miron Cuperman 2012-08-03 16:49:45 -07:00
parent 3560d18ea2
commit c4304fe07d

View File

@ -37,6 +37,7 @@ import java.net.InetSocketAddress;
import java.net.SocketAddress;
import java.util.*;
import java.util.concurrent.*;
import java.util.concurrent.atomic.AtomicInteger;
import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.base.Preconditions.checkNotNull;
@ -142,8 +143,8 @@ public class PeerGroup {
int connectionDelayMillis) {
this(params, chain, connectionDelayMillis, new ClientBootstrap(
new NioClientSocketChannelFactory(
Executors.newCachedThreadPool(),
Executors.newCachedThreadPool())));
Executors.newCachedThreadPool(new PeerGroupThreadFactory()),
Executors.newCachedThreadPool(new PeerGroupThreadFactory()))));
bootstrap.setPipelineFactory(makePipelineFactory(params, chain));
}
@ -908,4 +909,30 @@ public class PeerGroup {
}, MoreExecutors.sameThreadExecutor());
return future;
}
static class PeerGroupThreadFactory implements ThreadFactory {
static final AtomicInteger poolNumber = new AtomicInteger(1);
final ThreadGroup group;
final AtomicInteger threadNumber = new AtomicInteger(1);
final String namePrefix;
PeerGroupThreadFactory() {
group = Thread.currentThread().getThreadGroup();
namePrefix = "PeerGroup-" +
poolNumber.getAndIncrement() +
"-thread-";
}
public Thread newThread(Runnable r) {
Thread t = new Thread(group, r,
namePrefix + threadNumber.getAndIncrement(),
0);
// Lower the priority of the peer threads. This is to avoid competing with UI threads created by the API
// user when doing lots of work, like downloading the block chain. We select a priority level one lower
// than the parent thread, or the minimum.
t.setPriority(Math.max(Thread.MIN_PRIORITY, Thread.currentThread().getPriority() - 1));
t.setDaemon(true);
return t;
}
}
}