Implement EventExecutor.parent() in AbstractEventExecutor

Motivation:

EventExecutor.parent() and EventLoop.parent() almost always return a constant parent executor.  There's not much reason to let it implemented in subclasses.

Modifications:

- Implement AbstractEventExecutor.parent() with an additional contructor
- Add AbstractEventLoop so that subclasses extend AbstractEventLoop, which implements parent() appropriately
- Remove redundant parent() implementations in the subclasses
- Fix inspector warnings

Result:

Less duplication.
This commit is contained in:
Trustin Lee 2014-03-24 11:05:51 +09:00
parent ac5e838398
commit 007694b963
7 changed files with 64 additions and 36 deletions

View File

@ -29,6 +29,21 @@ import java.util.concurrent.TimeUnit;
*/
public abstract class AbstractEventExecutor extends AbstractExecutorService implements EventExecutor {
private final EventExecutorGroup parent;
protected AbstractEventExecutor() {
this(null);
}
protected AbstractEventExecutor(EventExecutorGroup parent) {
this.parent = parent;
}
@Override
public EventExecutorGroup parent() {
return parent;
}
@Override
public EventExecutor next() {
return this;

View File

@ -64,11 +64,6 @@ public final class GlobalEventExecutor extends AbstractEventExecutor {
delayedTaskQueue.add(purgeTask);
}
@Override
public EventExecutorGroup parent() {
return null;
}
/**
* Take the next {@link Runnable} from the task queue and so will block if no task is currently present.
*
@ -93,6 +88,7 @@ public final class GlobalEventExecutor extends AbstractEventExecutor {
try {
task = taskQueue.poll(delayNanos, TimeUnit.NANOSECONDS);
} catch (InterruptedException e) {
// Waken up.
return null;
}
} else {

View File

@ -30,11 +30,6 @@ public final class ImmediateEventExecutor extends AbstractEventExecutor {
// use static instance
}
@Override
public EventExecutorGroup parent() {
return null;
}
@Override
public boolean inEventLoop() {
return true;

View File

@ -70,7 +70,6 @@ public abstract class SingleThreadEventExecutor extends AbstractEventExecutor {
STATE_UPDATER = updater;
}
private final EventExecutorGroup parent;
private final Queue<Runnable> taskQueue;
final Queue<ScheduledFutureTask<?>> delayedTaskQueue = new PriorityQueue<ScheduledFutureTask<?>>();
@ -113,14 +112,13 @@ public abstract class SingleThreadEventExecutor extends AbstractEventExecutor {
* @param addTaskWakesUp {@code true} if and only if invocation of {@link #addTask(Runnable)} will wake up the
* executor thread
*/
protected SingleThreadEventExecutor(
EventExecutorGroup parent, Executor executor, boolean addTaskWakesUp) {
protected SingleThreadEventExecutor(EventExecutorGroup parent, Executor executor, boolean addTaskWakesUp) {
super(parent);
if (executor == null) {
throw new NullPointerException("executor");
}
this.parent = parent;
this.addTaskWakesUp = addTaskWakesUp;
this.executor = executor;
taskQueue = newTaskQueue();
@ -136,11 +134,6 @@ public abstract class SingleThreadEventExecutor extends AbstractEventExecutor {
return new LinkedBlockingQueue<Runnable>();
}
@Override
public EventExecutorGroup parent() {
return parent;
}
/**
* Interrupt the current running {@link Thread}.
*/
@ -203,6 +196,7 @@ public abstract class SingleThreadEventExecutor extends AbstractEventExecutor {
try {
task = taskQueue.poll(delayNanos, TimeUnit.NANOSECONDS);
} catch (InterruptedException e) {
// Waken up.
return null;
}
}

View File

@ -0,0 +1,41 @@
/*
* Copyright 2014 The Netty Project
*
* The Netty Project licenses this file to you under the Apache License,
* version 2.0 (the "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at:
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*/
package io.netty.channel;
import io.netty.util.concurrent.AbstractEventExecutor;
/**
* Skeletal implementation of {@link EventLoop}.
*/
public abstract class AbstractEventLoop extends AbstractEventExecutor implements EventLoop {
protected AbstractEventLoop() { }
protected AbstractEventLoop(EventLoopGroup parent) {
super(parent);
}
@Override
public EventLoopGroup parent() {
return (EventLoopGroup) super.parent();
}
@Override
public EventLoop next() {
return (EventLoop) super.next();
}
}

View File

@ -15,28 +15,21 @@
*/
package io.netty.channel;
import io.netty.util.concurrent.EventExecutorGroup;
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.
* 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)}
*/
protected SingleThreadEventLoop(EventLoopGroup parent, Executor executor, boolean addTaskWakesUp) {
super(parent, executor, addTaskWakesUp);
}
@ -75,7 +68,7 @@ public abstract class SingleThreadEventLoop extends SingleThreadEventExecutor im
}
/**
* Marker interface for {@linkRunnable} that will not trigger an {@link #wakeup(boolean)} in all cases.
* Marker interface for {@link Runnable} that will not trigger an {@link #wakeup(boolean)} in all cases.
*/
interface NonWakeupRunnable extends Runnable { }
}

View File

@ -15,20 +15,19 @@
*/
package io.netty.channel.embedded;
import io.netty.channel.AbstractEventLoop;
import io.netty.channel.Channel;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelPromise;
import io.netty.channel.DefaultChannelPromise;
import io.netty.channel.EventLoop;
import io.netty.channel.EventLoopGroup;
import io.netty.util.concurrent.AbstractEventExecutor;
import io.netty.util.concurrent.Future;
import java.util.ArrayDeque;
import java.util.Queue;
import java.util.concurrent.TimeUnit;
final class EmbeddedEventLoop extends AbstractEventExecutor implements EventLoop {
final class EmbeddedEventLoop extends AbstractEventLoop {
private final Queue<Runnable> tasks = new ArrayDeque<Runnable>(2);
@ -114,9 +113,4 @@ final class EmbeddedEventLoop extends AbstractEventExecutor implements EventLoop
public EventLoop next() {
return this;
}
@Override
public EventLoopGroup parent() {
return this;
}
}