
nl.inl.util.MainThreadExecutorService Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of blacklab-util Show documentation
Show all versions of blacklab-util Show documentation
Several utility functions used by BlackLab.
The newest version!
package nl.inl.util;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.AbstractExecutorService;
import java.util.concurrent.RejectedExecutionException;
import java.util.concurrent.TimeUnit;
/**
* Mock executorService that actually just runs all submitted tasks on the main
* thread. Note: unlike regular ExecutorService, this class is NOT thread-safe.
*/
public class MainThreadExecutorService extends AbstractExecutorService {
@FunctionalInterface
public interface RejectedExecutionHandler {
void rejectedExecution(Runnable r, MainThreadExecutorService e);
}
private final RejectedExecutionHandler handler;
private boolean shutdown;
MainThreadExecutorService() {
this((r, e) -> {
throw new RejectedExecutionException("Task " + r.toString() + " rejected from " + e.toString());
});
}
MainThreadExecutorService(RejectedExecutionHandler h) {
if (h == null)
throw new IllegalArgumentException("No RejectedExecutionHandler specified");
this.handler = h;
}
@Override
public boolean awaitTermination(long timeout, TimeUnit unit) {
shutdown = true;
return true;
}
@Override
public boolean isShutdown() {
return shutdown;
}
@Override
public boolean isTerminated() {
return shutdown;
}
@Override
public void shutdown() {
shutdown = true;
}
@Override
public List shutdownNow() {
return Collections.emptyList();
}
@Override
public void execute(Runnable command) {
if (shutdown)
handler.rejectedExecution(command, this);
command.run();
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy