io.mapsmessaging.utilities.threads.SimpleTaskScheduler Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of non_block_task_scheduler Show documentation
Show all versions of non_block_task_scheduler Show documentation
A non-block task queue and task scheduler
The newest version!
package io.mapsmessaging.utilities.threads;
import org.jetbrains.annotations.NotNull;
import java.util.Collection;
import java.util.List;
import java.util.concurrent.*;
public class SimpleTaskScheduler implements ScheduledExecutorService {
private static class Holder {
static final SimpleTaskScheduler INSTANCE = new SimpleTaskScheduler();
}
public static SimpleTaskScheduler getInstance() {
return Holder.INSTANCE;
}
private final ScheduledThreadPoolExecutor scheduler;
private SimpleTaskScheduler() {
scheduler = new ScheduledThreadPoolExecutor(10);
scheduler.setRemoveOnCancelPolicy(true);
scheduler.setExecuteExistingDelayedTasksAfterShutdownPolicy(false);
scheduler.setContinueExistingPeriodicTasksAfterShutdownPolicy(false);
}
@NotNull
@Override
public ScheduledFuture> schedule(@NotNull Runnable command, long delay, @NotNull TimeUnit unit) {
return scheduler.schedule(new InterceptedRunnable(command), delay, unit);
}
@NotNull
@Override
public ScheduledFuture schedule(@NotNull Callable callable, long delay, @NotNull TimeUnit unit) {
return scheduler.schedule(new InterceptedCallable<>(callable), delay, unit);
}
@NotNull
@Override
public ScheduledFuture> scheduleAtFixedRate(@NotNull Runnable command, long initialDelay, long period, @NotNull TimeUnit unit) {
return scheduler.scheduleAtFixedRate(command, initialDelay, period, unit);
}
@NotNull
@Override
public ScheduledFuture> scheduleWithFixedDelay(@NotNull Runnable command, long initialDelay, long period, @NotNull TimeUnit unit) {
return scheduler.scheduleWithFixedDelay(new InterceptedRunnable(command), initialDelay, period, unit);
}
@Override
public void shutdown() {
scheduler.shutdown();
}
@NotNull
@Override
public List shutdownNow() {
return scheduler.shutdownNow();
}
@Override
public boolean isShutdown() {
return scheduler.isShutdown();
}
@Override
public boolean isTerminated() {
return scheduler.isTerminated();
}
@Override
public boolean awaitTermination(long timeout, @NotNull TimeUnit unit) throws InterruptedException {
return scheduler.awaitTermination(timeout, unit);
}
@NotNull
@Override
public Future submit(@NotNull Callable task) {
return scheduler.submit(task);
}
@NotNull
@Override
public Future submit(@NotNull Runnable task, T result) {
return scheduler.submit(task, result);
}
@NotNull
@Override
public Future> submit(@NotNull Runnable task) {
return scheduler.submit(task);
}
@NotNull
@Override
public List> invokeAll(@NotNull Collection extends Callable> tasks) throws InterruptedException {
return scheduler.invokeAll(tasks);
}
@NotNull
@Override
public List> invokeAll(@NotNull Collection extends Callable> tasks, long timeout, @NotNull TimeUnit unit) throws InterruptedException {
return scheduler.invokeAll(tasks, timeout, unit);
}
@NotNull
@Override
public T invokeAny(@NotNull Collection extends Callable> tasks) throws InterruptedException, ExecutionException {
return scheduler.invokeAny(tasks);
}
@Override
public T invokeAny(@NotNull Collection extends Callable> tasks, long timeout, @NotNull TimeUnit unit) throws InterruptedException, ExecutionException, TimeoutException {
return scheduler.invokeAny(tasks, timeout, unit);
}
@Override
public void execute(@NotNull Runnable command) {
scheduler.execute(new InterceptedRunnable(command));
}
public long getTotalScheduled() {
return scheduler.getTaskCount();
}
public long getTotalExecuted() {
return scheduler.getCompletedTaskCount();
}
public int getDepth() {
return scheduler.getQueue().size();
}
public static final class InterceptedRunnable implements Runnable {
private final Runnable runnable;
public InterceptedRunnable(Runnable runnable) {
this.runnable = runnable;
}
@Override
public void run() {
runnable.run();
}
}
public static final class InterceptedCallable implements Callable {
private final Callable callable;
public InterceptedCallable(Callable callable) {
this.callable = callable;
}
@Override
public T call() throws Exception {
return callable.call();
}
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy