Replace synchronization with an lock free approach
This commit is contained in:
parent
bbd251baed
commit
9f712e3291
@ -16,13 +16,14 @@
|
|||||||
package org.jboss.netty.handler.execution;
|
package org.jboss.netty.handler.execution;
|
||||||
|
|
||||||
import java.util.IdentityHashMap;
|
import java.util.IdentityHashMap;
|
||||||
import java.util.LinkedList;
|
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
import java.util.WeakHashMap;
|
import java.util.WeakHashMap;
|
||||||
|
import java.util.concurrent.ConcurrentLinkedQueue;
|
||||||
import java.util.concurrent.ConcurrentMap;
|
import java.util.concurrent.ConcurrentMap;
|
||||||
import java.util.concurrent.Executor;
|
import java.util.concurrent.Executor;
|
||||||
import java.util.concurrent.ThreadFactory;
|
import java.util.concurrent.ThreadFactory;
|
||||||
import java.util.concurrent.TimeUnit;
|
import java.util.concurrent.TimeUnit;
|
||||||
|
import java.util.concurrent.atomic.AtomicBoolean;
|
||||||
|
|
||||||
import org.jboss.netty.channel.Channel;
|
import org.jboss.netty.channel.Channel;
|
||||||
import org.jboss.netty.channel.ChannelEvent;
|
import org.jboss.netty.channel.ChannelEvent;
|
||||||
@ -284,31 +285,35 @@ public class OrderedMemoryAwareThreadPoolExecutor extends
|
|||||||
}
|
}
|
||||||
|
|
||||||
private final class ChildExecutor implements Executor, Runnable {
|
private final class ChildExecutor implements Executor, Runnable {
|
||||||
private final LinkedList<Runnable> tasks = new LinkedList<Runnable>();
|
private final ConcurrentLinkedQueue<Runnable> tasks = new ConcurrentLinkedQueue<Runnable>();
|
||||||
|
private final AtomicBoolean isRunning = new AtomicBoolean(false);
|
||||||
|
|
||||||
ChildExecutor() {
|
ChildExecutor() {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void execute(Runnable command) {
|
public void execute(Runnable command) {
|
||||||
boolean needsExecution;
|
|
||||||
synchronized (tasks) {
|
|
||||||
needsExecution = tasks.isEmpty();
|
|
||||||
tasks.add(command);
|
tasks.add(command);
|
||||||
}
|
|
||||||
|
|
||||||
if (needsExecution) {
|
|
||||||
|
if (isRunning.get() == false) {
|
||||||
doUnorderedExecute(this);
|
doUnorderedExecute(this);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void run() {
|
public void run() {
|
||||||
|
// check if its already running by using CAS. If so just return here. So in the worst case the thread
|
||||||
|
// is executed and do nothing
|
||||||
|
if (isRunning.compareAndSet(false, true)) {
|
||||||
|
try {
|
||||||
Thread thread = Thread.currentThread();
|
Thread thread = Thread.currentThread();
|
||||||
for (;;) {
|
for (;;) {
|
||||||
final Runnable task;
|
final Runnable task = tasks.poll();
|
||||||
synchronized (tasks) {
|
// this should never happen but just in case check if
|
||||||
task = tasks.getFirst();
|
// the queue was empty
|
||||||
|
if (task == null) {
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
boolean ran = false;
|
boolean ran = false;
|
||||||
@ -323,13 +328,15 @@ public class OrderedMemoryAwareThreadPoolExecutor extends
|
|||||||
}
|
}
|
||||||
throw e;
|
throw e;
|
||||||
} finally {
|
} finally {
|
||||||
synchronized (tasks) {
|
|
||||||
tasks.removeFirst();
|
|
||||||
if (tasks.isEmpty()) {
|
if (tasks.isEmpty()) {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
} finally {
|
||||||
|
// set it back to not running
|
||||||
|
isRunning.set(false);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user