xapi.process.api.ConcurrentEnvironment Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of xapi-dev Show documentation
Show all versions of xapi-dev Show documentation
Everything needed to run a comprehensive dev environment.
Just type X_ and pick a service from autocomplete;
new dev modules will be added as they are built.
The only dev service not included in the uber jar is xapi-dev-maven,
as it includes all runtime dependencies of maven, adding ~4 seconds to build time,
and 6 megabytes to the final output jar size (without xapi-dev-maven, it's ~1MB).
The newest version!
package xapi.process.api;
import java.util.Iterator;
import java.util.concurrent.TimeoutException;
import javax.inject.Provider;
import xapi.log.X_Log;
import xapi.process.X_Process;
import xapi.util.X_Debug;
import static xapi.process.X_Process.now;
public abstract class ConcurrentEnvironment {
public static enum Priority {
High, Medium, Low
}
public static enum Strategy {
Block, FixedPeriod, DecayingPeriod, Recycle
}
private final Object synchro = new Object();
private static final double start = X_Process.now();
public void monitor(Priority priority, Provider gate, Runnable job) {
}
public boolean hasFinalies() {
return getFinally().iterator().hasNext();
}
public boolean hasDefers() {
return getDeferred().iterator().hasNext();
}
public boolean isEmpty() {
return (!(hasFinalies()||!hasDefers()));
}
public boolean flush(int timeout) {
if (isEmpty())
return true;
double max = now()+timeout;
try {
do {
if (hasFinalies()) {
System.out.println("run finallies");
runFinalies(max);
}
System.out.println("check timeout");
checkTimeouts(max);
Iterator iter = getDeferred().iterator();
while (iter.hasNext()) {
System.out.println("iterating job");
Runnable next;
synchronized (synchro) {
next = iter.next();
iter.remove();
}
next.run();
System.out.println("check finalies again");
if (hasFinalies()) {
System.out.println("has finalies");
runFinalies(max);
}
System.out.println("check timeout");
checkTimeouts(max);
}
}while(! isEmpty());
}catch (TimeoutException e) {
return false;
}finally {
if (X_Debug.isBenchmark()) {
X_Log.info("Spent "+(now()-(max-timeout))+" millis flushing environment");
}
}
return false;
}
protected void checkTimeouts(double max) throws TimeoutException {
if (now() > max)
throw new TimeoutException();
}
protected void runFinalies(double max) throws TimeoutException {
Iterator iter = getFinally().iterator();
while (iter.hasNext()) {
Runnable next;
synchronized (synchro) {
next = iter.next();
iter.remove();
}
next.run();
if (now() > max)
throw new TimeoutException();
}
}
public void scheduleFlush(int timeout) {
}
public boolean destroy(int timeout) {
double max = now() + timeout;
while (!isEmpty()) {
int left = (int)(max - now());
if (left < 1)
return true;
flush(left);
}
return false;
}
public double startTime() {
return start;
}
public abstract Iterable getDeferred();
public abstract Iterable getFinally();
public abstract Iterable getThreads();
public abstract void pushDeferred(Runnable cmd);
public abstract void pushEventually(Runnable cmd);
public abstract void pushFinally(Runnable cmd);
public abstract void pushThread(Thread childThread);
}