diff --git a/transport/src/main/java/io/netty/channel/MultithreadEventExecutor.java b/transport/src/main/java/io/netty/channel/MultithreadEventExecutor.java index 9434c19761..1087698142 100644 --- a/transport/src/main/java/io/netty/channel/MultithreadEventExecutor.java +++ b/transport/src/main/java/io/netty/channel/MultithreadEventExecutor.java @@ -5,7 +5,6 @@ import java.util.Collections; import java.util.List; import java.util.concurrent.Callable; import java.util.concurrent.ExecutionException; -import java.util.concurrent.Executors; import java.util.concurrent.Future; import java.util.concurrent.ScheduledFuture; import java.util.concurrent.ThreadFactory; @@ -15,8 +14,8 @@ import java.util.concurrent.atomic.AtomicInteger; public abstract class MultithreadEventExecutor implements EventExecutor { - protected static final int DEFAULT_POOL_SIZE = Runtime.getRuntime().availableProcessors() * 2; - protected static final ThreadFactory DEFAULT_THREAD_FACTORY = Executors.defaultThreadFactory(); + private static final int DEFAULT_POOL_SIZE = Runtime.getRuntime().availableProcessors() * 2; + private static final AtomicInteger poolId = new AtomicInteger(); private final EventExecutor[] children; private final AtomicInteger childIndex = new AtomicInteger(); @@ -32,7 +31,7 @@ public abstract class MultithreadEventExecutor implements EventExecutor { } protected MultithreadEventExecutor(int nThreads, Object... args) { - this(nThreads, DEFAULT_THREAD_FACTORY, args); + this(nThreads, null, args); } protected MultithreadEventExecutor(int nThreads, ThreadFactory threadFactory, Object... args) { @@ -40,8 +39,9 @@ public abstract class MultithreadEventExecutor implements EventExecutor { throw new IllegalArgumentException(String.format( "nThreads: %d (expected: > 0)", nThreads)); } + if (threadFactory == null) { - throw new NullPointerException("threadFactory"); + threadFactory = new DefaultThreadFactory(); } children = new SingleThreadEventExecutor[nThreads]; @@ -201,4 +201,31 @@ public abstract class MultithreadEventExecutor implements EventExecutor { } return loop; } + + private final class DefaultThreadFactory implements ThreadFactory { + private final AtomicInteger nextId = new AtomicInteger(); + private final String prefix; + + DefaultThreadFactory() { + String typeName = MultithreadEventExecutor.this.getClass().getSimpleName(); + typeName = "" + Character.toLowerCase(typeName.charAt(0)) + typeName.substring(1); + prefix = typeName + '-' + poolId.incrementAndGet() + '-'; + } + + @Override + public Thread newThread(Runnable r) { + Thread t = new Thread(r, prefix + nextId.incrementAndGet()); + try { + if (t.isDaemon()) { + t.setDaemon(false); + } + if (t.getPriority() != Thread.MAX_PRIORITY) { + t.setPriority(Thread.MAX_PRIORITY); + } + } catch (Exception ignored) { + // Doesn't matter even if failed to set. + } + return t; + } + } } diff --git a/transport/src/main/java/io/netty/channel/SingleThreadEventExecutor.java b/transport/src/main/java/io/netty/channel/SingleThreadEventExecutor.java index 5b44e581cd..85b650be71 100644 --- a/transport/src/main/java/io/netty/channel/SingleThreadEventExecutor.java +++ b/transport/src/main/java/io/netty/channel/SingleThreadEventExecutor.java @@ -16,7 +16,6 @@ import java.util.concurrent.BlockingQueue; import java.util.concurrent.Callable; import java.util.concurrent.DelayQueue; import java.util.concurrent.Delayed; -import java.util.concurrent.Executors; import java.util.concurrent.FutureTask; import java.util.concurrent.RejectedExecutionException; import java.util.concurrent.ScheduledFuture; @@ -68,10 +67,6 @@ public abstract class SingleThreadEventExecutor extends AbstractExecutorService private long lastCheckTimeNanos; private long lastPurgeTimeNanos; - protected SingleThreadEventExecutor() { - this(Executors.defaultThreadFactory()); - } - protected SingleThreadEventExecutor(ThreadFactory threadFactory) { thread = threadFactory.newThread(new Runnable() { @Override diff --git a/transport/src/main/java/io/netty/channel/SingleThreadEventLoop.java b/transport/src/main/java/io/netty/channel/SingleThreadEventLoop.java index 8623e5e13d..c32a102beb 100644 --- a/transport/src/main/java/io/netty/channel/SingleThreadEventLoop.java +++ b/transport/src/main/java/io/netty/channel/SingleThreadEventLoop.java @@ -4,8 +4,6 @@ import java.util.concurrent.ThreadFactory; public abstract class SingleThreadEventLoop extends SingleThreadEventExecutor implements EventLoop { - protected SingleThreadEventLoop() {} - protected SingleThreadEventLoop(ThreadFactory threadFactory) { super(threadFactory); }