Let background computations abort for browse command

This commit is contained in:
Rene Pfeuffer
2019-12-12 11:47:03 +01:00
parent f7dc89ee81
commit 8df43e7b4e
12 changed files with 122 additions and 29 deletions

View File

@@ -14,7 +14,7 @@ public final class SyncAsyncExecutors {
public static SyncAsyncExecutor synchronousExecutor() {
return new SyncAsyncExecutor() {
@Override
public ExecutionType execute(Consumer<ExecutionType> runnable) {
public ExecutionType execute(Consumer<ExecutionType> runnable, Runnable abortionFallback) {
runnable.accept(SYNCHRONOUS);
return SYNCHRONOUS;
}
@@ -32,7 +32,7 @@ public final class SyncAsyncExecutors {
return new SyncAsyncExecutor() {
@Override
public ExecutionType execute(Consumer<ExecutionType> runnable) {
public ExecutionType execute(Consumer<ExecutionType> runnable, Runnable abortionFallback) {
executor.execute(() -> runnable.accept(ASYNCHRONOUS));
return ASYNCHRONOUS;
}
@@ -45,12 +45,13 @@ public final class SyncAsyncExecutors {
}
public static AsyncExecutorStepper stepperAsynchronousExecutor() {
Executor executor = Executors.newSingleThreadExecutor();
Semaphore enterSemaphore = new Semaphore(0);
Semaphore exitSemaphore = new Semaphore(0);
return new AsyncExecutorStepper() {
Executor executor = Executors.newSingleThreadExecutor();
Semaphore enterSemaphore = new Semaphore(0);
Semaphore exitSemaphore = new Semaphore(0);
boolean timedOut = false;
@Override
public void close() {
enterSemaphore.release(Integer.MAX_VALUE/2);
@@ -58,15 +59,19 @@ public final class SyncAsyncExecutors {
}
@Override
public ExecutionType execute(Consumer<ExecutionType> runnable) {
public ExecutionType execute(Consumer<ExecutionType> runnable, Runnable abortionFallback) {
executor.execute(() -> {
try {
enterSemaphore.acquire();
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
runnable.accept(ASYNCHRONOUS);
exitSemaphore.release();
if (timedOut) {
abortionFallback.run();
} else {
runnable.accept(ASYNCHRONOUS);
exitSemaphore.release();
}
});
return ASYNCHRONOUS;
}
@@ -81,6 +86,12 @@ public final class SyncAsyncExecutors {
}
}
@Override
public void timeout() {
timedOut = true;
close();
}
@Override
public boolean hasExecutedAllSynchronously() {
return true;
@@ -90,5 +101,7 @@ public final class SyncAsyncExecutors {
public interface AsyncExecutorStepper extends SyncAsyncExecutor, Closeable {
void next();
void timeout();
}
}