A little bit of optimization

This commit is contained in:
Trustin Lee 2008-09-26 00:43:14 +00:00
parent e1f4053d41
commit 824e0034c4

View File

@ -22,10 +22,7 @@
*/ */
package org.jboss.netty.handler.execution; package org.jboss.netty.handler.execution;
import java.util.HashSet;
import java.util.LinkedList; import java.util.LinkedList;
import java.util.Queue;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap; import java.util.concurrent.ConcurrentMap;
import java.util.concurrent.Executor; import java.util.concurrent.Executor;
@ -172,19 +169,21 @@ public class OrderedMemoryAwareThreadPoolExecutor extends
} }
private class ChildExecutor implements Executor, Runnable { private class ChildExecutor implements Executor, Runnable {
private final Set<ChildExecutor> runningChildren = new HashSet<ChildExecutor>(); private final LinkedList<Runnable> tasks = new LinkedList<Runnable>();
private final Queue<Runnable> tasks = new LinkedList<Runnable>();
ChildExecutor() { ChildExecutor() {
super(); super();
} }
public void execute(Runnable command) { public void execute(Runnable command) {
boolean needsExecution;
synchronized (tasks) { synchronized (tasks) {
needsExecution = tasks.isEmpty();
tasks.add(command); tasks.add(command);
if (tasks.size() == 1 && runningChildren.add(this)) { }
doUnorderedExecute(this);
} if (needsExecution) {
doUnorderedExecute(this);
} }
} }
@ -192,13 +191,19 @@ public class OrderedMemoryAwareThreadPoolExecutor extends
for (;;) { for (;;) {
final Runnable task; final Runnable task;
synchronized (tasks) { synchronized (tasks) {
task = tasks.poll(); task = tasks.getFirst();
if (task == null) { }
runningChildren.remove(this);
return; try {
task.run();
} finally {
synchronized (tasks) {
tasks.removeFirst();
if (tasks.isEmpty()) {
break;
}
} }
} }
task.run();
} }
} }
} }