From 9176cea8fc664a4d63df794d44a84fc09cc2d6fd Mon Sep 17 00:00:00 2001 From: Vladimir Krivosheev Date: Wed, 21 Aug 2013 11:19:27 +0200 Subject: [PATCH] =?UTF-8?q?restore=20old=20constructors=20=E2=80=94=20acce?= =?UTF-8?q?pt=20ThreadFactory=20[#1762]=20ability=20to=20use=20Executor=20?= =?UTF-8?q?instead=20of=20ThreadFactory?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../concurrent/SingleThreadEventExecutor.java | 14 +++++++++++ .../concurrent/ThreadPerTaskExecutor.java | 5 +++- .../netty/channel/SingleThreadEventLoop.java | 8 +++++++ .../ThreadPerChannelEventLoopGroup.java | 23 ++++++++++++++++--- .../netty/channel/oio/OioEventLoopGroup.java | 3 +-- .../channel/SingleThreadEventLoopTest.java | 5 ++-- 6 files changed, 49 insertions(+), 9 deletions(-) diff --git a/common/src/main/java/io/netty/util/concurrent/SingleThreadEventExecutor.java b/common/src/main/java/io/netty/util/concurrent/SingleThreadEventExecutor.java index 6dbb5c94aab..33aa42c12a6 100644 --- a/common/src/main/java/io/netty/util/concurrent/SingleThreadEventExecutor.java +++ b/common/src/main/java/io/netty/util/concurrent/SingleThreadEventExecutor.java @@ -32,6 +32,7 @@ import java.util.concurrent.LinkedBlockingQueue; import java.util.concurrent.RejectedExecutionException; import java.util.concurrent.Semaphore; +import java.util.concurrent.ThreadFactory; import java.util.concurrent.TimeUnit; /** @@ -76,6 +77,19 @@ public void run() { private final Promise terminationFuture = new DefaultPromise(GlobalEventExecutor.INSTANCE); + /** + * Create a new instance + * + * @param parent the {@link EventExecutorGroup} which is the parent of this instance and belongs to it + * @param threadFactory the {@link ThreadFactory} which will be used for the used {@link Thread} + * @param addTaskWakesUp {@code true} if and only if invocation of {@link #addTask(Runnable)} will wake up the + * executor thread + */ + protected SingleThreadEventExecutor( + EventExecutorGroup parent, ThreadFactory threadFactory, boolean addTaskWakesUp) { + this(parent, new ThreadPerTaskExecutor(threadFactory), addTaskWakesUp); + } + /** * Create a new instance * diff --git a/common/src/main/java/io/netty/util/concurrent/ThreadPerTaskExecutor.java b/common/src/main/java/io/netty/util/concurrent/ThreadPerTaskExecutor.java index a08ddb2bc46..21210ae0673 100644 --- a/common/src/main/java/io/netty/util/concurrent/ThreadPerTaskExecutor.java +++ b/common/src/main/java/io/netty/util/concurrent/ThreadPerTaskExecutor.java @@ -18,10 +18,13 @@ import java.util.concurrent.Executor; import java.util.concurrent.ThreadFactory; -public class ThreadPerTaskExecutor implements Executor { +public final class ThreadPerTaskExecutor implements Executor { private final ThreadFactory threadFactory; public ThreadPerTaskExecutor(ThreadFactory threadFactory) { + if (threadFactory == null) { + throw new NullPointerException("threadFactory"); + } this.threadFactory = threadFactory; } diff --git a/transport/src/main/java/io/netty/channel/SingleThreadEventLoop.java b/transport/src/main/java/io/netty/channel/SingleThreadEventLoop.java index b46cd5fd9d5..12bed9ae74b 100644 --- a/transport/src/main/java/io/netty/channel/SingleThreadEventLoop.java +++ b/transport/src/main/java/io/netty/channel/SingleThreadEventLoop.java @@ -19,6 +19,7 @@ import io.netty.util.concurrent.SingleThreadEventExecutor; import java.util.concurrent.Executor; +import java.util.concurrent.ThreadFactory; /** * Abstract base class for {@link EventLoop}'s that execute all its submitted tasks in a single thread. @@ -26,6 +27,13 @@ */ public abstract class SingleThreadEventLoop extends SingleThreadEventExecutor implements EventLoop { + /** + * @see {@link SingleThreadEventExecutor#SingleThreadEventExecutor(EventExecutorGroup, ThreadFactory, boolean)} + */ + protected SingleThreadEventLoop(EventLoopGroup parent, ThreadFactory threadFactory, boolean addTaskWakesUp) { + super(parent, threadFactory, addTaskWakesUp); + } + /** * @see {@link SingleThreadEventExecutor#SingleThreadEventExecutor(EventExecutorGroup, Executor, boolean)} */ diff --git a/transport/src/main/java/io/netty/channel/ThreadPerChannelEventLoopGroup.java b/transport/src/main/java/io/netty/channel/ThreadPerChannelEventLoopGroup.java index 86c85da0977..25d89ee60f6 100644 --- a/transport/src/main/java/io/netty/channel/ThreadPerChannelEventLoopGroup.java +++ b/transport/src/main/java/io/netty/channel/ThreadPerChannelEventLoopGroup.java @@ -36,6 +36,7 @@ import java.util.concurrent.Executor; import java.util.concurrent.Executors; import java.util.concurrent.RejectedExecutionException; +import java.util.concurrent.ThreadFactory; import java.util.concurrent.TimeUnit; /** @@ -80,7 +81,7 @@ protected ThreadPerChannelEventLoopGroup() { * Use {@code 0} to use no limit */ protected ThreadPerChannelEventLoopGroup(int maxChannels) { - this(maxChannels, new ThreadPerTaskExecutor(Executors.defaultThreadFactory())); + this(maxChannels, Executors.defaultThreadFactory()); } /** @@ -91,7 +92,23 @@ protected ThreadPerChannelEventLoopGroup(int maxChannels) { * {@link ChannelException} on the {@link #register(Channel)} and * {@link #register(Channel, ChannelPromise)} method. * Use {@code 0} to use no limit - * @param executor the {@link Executor} used to create new {@link Thread} instances that handle the + * @param threadFactory the {@link ThreadFactory} used to create new {@link Thread} instances that handle the + * registered {@link Channel}s + * @param args arguments which will passed to each {@link #newChild(Object...)} call. + */ + protected ThreadPerChannelEventLoopGroup(int maxChannels, ThreadFactory threadFactory, Object... args) { + this(maxChannels, new ThreadPerTaskExecutor(threadFactory), args); + } + + /** + * Create a new {@link ThreadPerChannelEventLoopGroup}. + * + * @param maxChannels the maximum number of channels to handle with this instance. Once you try to register + * a new {@link Channel} and the maximum is exceed it will throw an + * {@link ChannelException} on the {@link #register(Channel)} and + * {@link #register(Channel, ChannelPromise)} method. + * Use {@code 0} to use no limit + * @param executor the {@link Executor} used to create new {@link Thread} instances that handle the * registered {@link Channel}s * @param args arguments which will passed to each {@link #newChild(Object...)} call. */ @@ -101,7 +118,7 @@ protected ThreadPerChannelEventLoopGroup(int maxChannels, Executor executor, Obj "maxChannels: %d (expected: >= 0)", maxChannels)); } if (executor == null) { - throw new NullPointerException("threadFactory"); + throw new NullPointerException("executor"); } if (args == null) { diff --git a/transport/src/main/java/io/netty/channel/oio/OioEventLoopGroup.java b/transport/src/main/java/io/netty/channel/oio/OioEventLoopGroup.java index 43f9c6a9ea8..e803f8b325e 100644 --- a/transport/src/main/java/io/netty/channel/oio/OioEventLoopGroup.java +++ b/transport/src/main/java/io/netty/channel/oio/OioEventLoopGroup.java @@ -22,7 +22,6 @@ import io.netty.channel.EventLoop; import io.netty.channel.EventLoopGroup; import io.netty.channel.ThreadPerChannelEventLoopGroup; -import io.netty.util.concurrent.ThreadPerTaskExecutor; import java.util.concurrent.Executor; import java.util.concurrent.Executors; @@ -81,6 +80,6 @@ public OioEventLoopGroup(int maxChannels, Executor executor) { * registered {@link Channel}s */ public OioEventLoopGroup(int maxChannels, ThreadFactory threadFactory) { - super(maxChannels, new ThreadPerTaskExecutor(threadFactory)); + super(maxChannels, threadFactory); } } diff --git a/transport/src/test/java/io/netty/channel/SingleThreadEventLoopTest.java b/transport/src/test/java/io/netty/channel/SingleThreadEventLoopTest.java index c05597996fd..6441fbc76ef 100644 --- a/transport/src/test/java/io/netty/channel/SingleThreadEventLoopTest.java +++ b/transport/src/test/java/io/netty/channel/SingleThreadEventLoopTest.java @@ -17,7 +17,6 @@ import io.netty.channel.local.LocalChannel; import io.netty.util.concurrent.EventExecutor; -import io.netty.util.concurrent.ThreadPerTaskExecutor; import org.junit.After; import org.junit.Before; import org.junit.Test; @@ -402,7 +401,7 @@ private static class SingleThreadEventLoopA extends SingleThreadEventLoop { final AtomicInteger cleanedUp = new AtomicInteger(); SingleThreadEventLoopA() { - super(null, new ThreadPerTaskExecutor(Executors.defaultThreadFactory()), true); + super(null, Executors.defaultThreadFactory(), true); } @Override @@ -429,7 +428,7 @@ protected void cleanup() { private static class SingleThreadEventLoopB extends SingleThreadEventLoop { SingleThreadEventLoopB() { - super(null, new ThreadPerTaskExecutor(Executors.defaultThreadFactory()), false); + super(null, Executors.defaultThreadFactory(), false); } @Override