From c4304fe07d365d77e4e2bf83b5fd44a4f494139b Mon Sep 17 00:00:00 2001 From: Miron Cuperman Date: Fri, 3 Aug 2012 16:49:45 -0700 Subject: [PATCH] Bring back PeerGroupThreadFactory --- .../com/google/bitcoin/core/PeerGroup.java | 31 +++++++++++++++++-- 1 file changed, 29 insertions(+), 2 deletions(-) diff --git a/core/src/main/java/com/google/bitcoin/core/PeerGroup.java b/core/src/main/java/com/google/bitcoin/core/PeerGroup.java index 0d96015f6..b0c38331d 100644 --- a/core/src/main/java/com/google/bitcoin/core/PeerGroup.java +++ b/core/src/main/java/com/google/bitcoin/core/PeerGroup.java @@ -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; + } + } }