From b9e869545cffbe2f2ff259085205fbcb5f25aa2d Mon Sep 17 00:00:00 2001 From: norman Date: Mon, 12 Dec 2011 08:21:53 +0100 Subject: [PATCH] Add special subclasses of MemoryAwareThreadPoolExecutor and OrderedMemoryAwareThreadPoolExecutor which should be used for SEDA processing. See #111 --- .../SedaMemoryAwareThreadPoolExecutor.java | 61 +++++++++++++++++++ ...aOrderedMemoryAwareThreadPoolExecutor.java | 59 ++++++++++++++++++ .../execution/seda/SimpleSedaExecutor.java | 4 +- 3 files changed, 122 insertions(+), 2 deletions(-) create mode 100644 src/main/java/io/netty/handler/execution/seda/SedaMemoryAwareThreadPoolExecutor.java create mode 100644 src/main/java/io/netty/handler/execution/seda/SedaOrderedMemoryAwareThreadPoolExecutor.java diff --git a/src/main/java/io/netty/handler/execution/seda/SedaMemoryAwareThreadPoolExecutor.java b/src/main/java/io/netty/handler/execution/seda/SedaMemoryAwareThreadPoolExecutor.java new file mode 100644 index 0000000000..1d32e2afb9 --- /dev/null +++ b/src/main/java/io/netty/handler/execution/seda/SedaMemoryAwareThreadPoolExecutor.java @@ -0,0 +1,61 @@ +/* + * Copyright 2011 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.handler.execution.seda; + +import java.util.concurrent.ThreadFactory; +import java.util.concurrent.TimeUnit; + +import io.netty.handler.execution.MemoryAwareThreadPoolExecutor; +import io.netty.util.ObjectSizeEstimator; + +/** + * Subclass of {@link MemoryAwareThreadPoolExecutor} which has all the same semantics as {@link MemoryAwareThreadPoolExecutor}. The only difference is that it will not keep track of the memory usage + * of downstream events. + * + * + * For more details see {@link MemoryAwareThreadPoolExecutor} + * + */ +public class SedaMemoryAwareThreadPoolExecutor extends MemoryAwareThreadPoolExecutor{ + + public SedaMemoryAwareThreadPoolExecutor(int corePoolSize, long maxChannelMemorySize, long maxTotalMemorySize, long keepAliveTime, TimeUnit unit, ObjectSizeEstimator objectSizeEstimator, ThreadFactory threadFactory) { + super(corePoolSize, maxChannelMemorySize, maxTotalMemorySize, keepAliveTime, unit, objectSizeEstimator, threadFactory); + } + + public SedaMemoryAwareThreadPoolExecutor(int corePoolSize, long maxChannelMemorySize, long maxTotalMemorySize, long keepAliveTime, TimeUnit unit, ThreadFactory threadFactory) { + super(corePoolSize, maxChannelMemorySize, maxTotalMemorySize, keepAliveTime, unit, threadFactory); + } + + public SedaMemoryAwareThreadPoolExecutor(int corePoolSize, long maxChannelMemorySize, long maxTotalMemorySize, long keepAliveTime, TimeUnit unit) { + super(corePoolSize, maxChannelMemorySize, maxTotalMemorySize, keepAliveTime, unit); + } + + public SedaMemoryAwareThreadPoolExecutor(int corePoolSize, long maxChannelMemorySize, long maxTotalMemorySize) { + super(corePoolSize, maxChannelMemorySize, maxTotalMemorySize); + } + + /** + * Don't count if {@link Runnable} is an instance of {@link ChannelDownstreamEventRunnable} + */ + @Override + protected boolean shouldCount(Runnable task) { + if (!(task instanceof ChannelDownstreamEventRunnable)) { + return super.shouldCount(task); + } + return false; + } + +} diff --git a/src/main/java/io/netty/handler/execution/seda/SedaOrderedMemoryAwareThreadPoolExecutor.java b/src/main/java/io/netty/handler/execution/seda/SedaOrderedMemoryAwareThreadPoolExecutor.java new file mode 100644 index 0000000000..c1d1ea4b8f --- /dev/null +++ b/src/main/java/io/netty/handler/execution/seda/SedaOrderedMemoryAwareThreadPoolExecutor.java @@ -0,0 +1,59 @@ +/* + * Copyright 2011 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.handler.execution.seda; + +import java.util.concurrent.ThreadFactory; +import java.util.concurrent.TimeUnit; + +import io.netty.handler.execution.OrderedMemoryAwareThreadPoolExecutor; +import io.netty.util.ObjectSizeEstimator; + +/** + * Subclass of {@link OrderedMemoryAwareThreadPoolExecutor} which has all the same semantics as {@link OrderedMemoryAwareThreadPoolExecutor}. The only difference is that it will not keep track of the memory usage + * of downstream events . + * + * For more details see {@link OrderedMemoryAwareThreadPoolExecutor} + * + */ +public class SedaOrderedMemoryAwareThreadPoolExecutor extends OrderedMemoryAwareThreadPoolExecutor{ + + public SedaOrderedMemoryAwareThreadPoolExecutor(int corePoolSize, long maxChannelMemorySize, long maxTotalMemorySize, long keepAliveTime, TimeUnit unit, ObjectSizeEstimator objectSizeEstimator, ThreadFactory threadFactory) { + super(corePoolSize, maxChannelMemorySize, maxTotalMemorySize, keepAliveTime, unit, objectSizeEstimator, threadFactory); + } + + public SedaOrderedMemoryAwareThreadPoolExecutor(int corePoolSize, long maxChannelMemorySize, long maxTotalMemorySize, long keepAliveTime, TimeUnit unit, ThreadFactory threadFactory) { + super(corePoolSize, maxChannelMemorySize, maxTotalMemorySize, keepAliveTime, unit, threadFactory); + } + + public SedaOrderedMemoryAwareThreadPoolExecutor(int corePoolSize, long maxChannelMemorySize, long maxTotalMemorySize, long keepAliveTime, TimeUnit unit) { + super(corePoolSize, maxChannelMemorySize, maxTotalMemorySize, keepAliveTime, unit); + } + + public SedaOrderedMemoryAwareThreadPoolExecutor(int corePoolSize, long maxChannelMemorySize, long maxTotalMemorySize) { + super(corePoolSize, maxChannelMemorySize, maxTotalMemorySize); + } + + /** + * Don't count if {@link Runnable} is an instance of {@link ChannelDownstreamEventRunnable} + */ + @Override + protected boolean shouldCount(Runnable task) { + if (!(task instanceof ChannelDownstreamEventRunnable)) { + return super.shouldCount(task); + } + return false; + } +} diff --git a/src/main/java/io/netty/handler/execution/seda/SimpleSedaExecutor.java b/src/main/java/io/netty/handler/execution/seda/SimpleSedaExecutor.java index 17a261a236..66b44dd93e 100644 --- a/src/main/java/io/netty/handler/execution/seda/SimpleSedaExecutor.java +++ b/src/main/java/io/netty/handler/execution/seda/SimpleSedaExecutor.java @@ -18,13 +18,13 @@ package io.netty.handler.execution.seda; import java.util.concurrent.Executor; import io.netty.handler.execution.ChannelEventRunnable; -import io.netty.handler.execution.OrderedMemoryAwareThreadPoolExecutor; import io.netty.util.internal.ExecutorUtil; /** * {@link SedaExecutor} which use two different {@link Executor}'s. One is used for upstream events and one for downstream events. * - * You should use an {@link OrderedMemoryAwareThreadPoolExecutor} if you care about the order of thread-execution. In most cases this should be the case + * You should use an {@link SedaOrderedMemoryAwareThreadPoolExecutor} if you care about the order of thread-execution. In most cases this should be the case + * * * */