io.nextop.util.MoreExecutors Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of java-common Show documentation
Show all versions of java-common Show documentation
Platform-agnostic Java core for Nextop
The newest version!
package io.nextop.util;
import javax.annotation.Nullable;
import java.util.LinkedList;
import java.util.Queue;
import java.util.concurrent.Executor;
public class MoreExecutors {
/** serially executes jobs on one of the threads in the delegate */
public static Executor serial(Executor delegate) {
return new SerialExecutor(delegate);
}
private static final class SerialExecutor implements Executor {
private final Executor delegate;
private final Queue queue = new LinkedList();
private final Object mutex = new Object();
@Nullable
private Runnable active = null;
SerialExecutor(Executor delegate) {
this.delegate = delegate;
}
@Override
public void execute(Runnable command) {
@Nullable Runnable pending;
synchronized (mutex) {
queue.add(command);
if (null == active) {
active = new Reactor();
pending = active;
} else {
pending = null;
}
}
if (null != pending) {
delegate.execute(pending);
}
}
private final class Reactor implements Runnable {
@Override
public void run() {
@Nullable Runnable r;
do {
synchronized (mutex) {
r = queue.poll();
if (null == r) {
active = null;
}
}
if (null != r) {
r.run();
}
} while (null != r);
}
};
}
}