Add a DummyRunExecutorService

This commit is contained in:
tom lee
2022-02-12 21:57:09 +08:00
parent cf1c371311
commit fbbccf4739
@@ -0,0 +1,133 @@
package com.seibel.lod.core.util;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.concurrent.Callable;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
public class DummyRunExecutorService implements ExecutorService {
private boolean shutdownCalled = false;
@Override
public void execute(Runnable command)
{
command.run();
}
@Override
public void shutdown()
{
shutdownCalled = true;
}
@Override
public List<Runnable> shutdownNow()
{
shutdownCalled = true;
return List.of();
}
@Override
public boolean isShutdown()
{
return shutdownCalled;
}
@Override
public boolean isTerminated()
{
return shutdownCalled;
}
@Override
public boolean awaitTermination(long timeout, TimeUnit unit)
{
shutdownCalled = true;
return true;
}
@Override
public <T> Future<T> submit(Callable<T> task)
{
try
{
return CompletableFuture.completedFuture(task.call());
}
catch (Throwable e)
{
return CompletableFuture.failedFuture(e);
}
}
@Override
public <T> Future<T> submit(Runnable task, T result)
{
try
{
task.run();
return CompletableFuture.completedFuture(result);
}
catch (Throwable e)
{
return CompletableFuture.failedFuture(e);
}
}
@Override
public Future<?> submit(Runnable task)
{
try
{
task.run();
return CompletableFuture.<Void>completedFuture(null);
}
catch (Throwable e)
{
return CompletableFuture.<Void>failedFuture(e);
}
}
@Override
public <T> List<Future<T>> invokeAll(Collection<? extends Callable<T>> tasks)
{
List<Future<T>> futures = new ArrayList<Future<T>>(tasks.size());
for (Callable<T> t : tasks) {
futures.add(submit(t));
}
return futures;
}
@Override
public <T> List<Future<T>> invokeAll(Collection<? extends Callable<T>> tasks, long timeout, TimeUnit unit)
{
return invokeAll(tasks);
}
@Override
public <T> T invokeAny(Collection<? extends Callable<T>> tasks) throws ExecutionException
{
Throwable latestE = null;
for (Callable<T> t : tasks) {
try {
return t.call();
}
catch (Throwable e)
{
latestE = e;
}
}
throw new ExecutionException(latestE);
}
@Override
public <T> T invokeAny(Collection<? extends Callable<T>> tasks, long timeout, TimeUnit unit) throws ExecutionException
{
return invokeAny(tasks);
}
}