
com.github.akurilov.fiber4j.FibersExecutorTask Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of fiber4j Show documentation
Show all versions of fiber4j Show documentation
Co-operative multitasking utility
The newest version!
package com.github.akurilov.fiber4j;
import com.github.akurilov.commons.concurrent.AsyncRunnableBase;
import java.util.Queue;
import java.util.concurrent.locks.LockSupport;
import java.util.logging.Level;
import java.util.logging.Logger;
public final class FibersExecutorTask
extends AsyncRunnableBase
implements Runnable {
private final static Logger LOG = Logger.getLogger(FibersExecutorTask.class.getName());
private final Queue fibers;
private final boolean backgroundFlag;
public FibersExecutorTask(
final Queue fibers, final boolean backgroundFlag
) {
this.fibers = fibers;
this.backgroundFlag = backgroundFlag;
}
@Override
public final void run() {
while(isStarted()) {
if(fibers.size() == 0) {
try {
Thread.sleep(1);
} catch(final InterruptedException e) {
break;
}
} else {
for(final Fiber nextFiber : fibers) {
try {
if(nextFiber.isStarted() || nextFiber.isShutdown()) {
nextFiber.invoke();
}
} catch(final RuntimeException e) {
throw e; // don't catch the unchecked exceptions
} catch(final Throwable t) {
LOG.log(Level.WARNING, "Fiber \"" + nextFiber + "\" failed", t);
}
if(backgroundFlag) {
LockSupport.parkNanos(1);
}
}
}
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy