Skip to content

Commit

Permalink
restore old constructors — accept ThreadFactory
Browse files Browse the repository at this point in the history
[#1762] ability to use Executor instead of ThreadFactory
  • Loading branch information
develar committed Aug 21, 2013
1 parent 853960a commit 9176cea
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 9 deletions.
Expand Up @@ -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;

/**
Expand Down Expand Up @@ -76,6 +77,19 @@ public void run() {

private final Promise<?> terminationFuture = new DefaultPromise<Void>(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
*
Expand Down
Expand Up @@ -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;
}

Expand Down
Expand Up @@ -19,13 +19,21 @@
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.
*
*/
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)}
*/
Expand Down
Expand Up @@ -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;

/**
Expand Down Expand Up @@ -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());
}

/**
Expand All @@ -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.
*/
Expand All @@ -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) {
Expand Down
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
}
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down

0 comments on commit 9176cea

Please sign in to comment.