do not use interruptions to abort execution

This commit is contained in:
2023-02-28 20:17:25 +01:00
parent eb9904a30b
commit be550ebac5
8 changed files with 108 additions and 24 deletions

View File

@@ -12,8 +12,19 @@ public class AbortException extends RuntimeException {
super(cause);
}
public static ThreadLocal<JobAborter> IS_CANCELLED = new ThreadLocal<>();
public static void setAborter(final JobAborter jobAborter) {
IS_CANCELLED.set(jobAborter);
}
public static void cancel() {
IS_CANCELLED.get().cancel();
}
public static void abortIfInterrupted() throws AbortException {
if (Thread.interrupted()) {
final JobAborter aborter = IS_CANCELLED.get();
if (aborter != null && Boolean.TRUE.equals(IS_CANCELLED.get().isCancelled())) {
throw new AbortException();
}
}

View File

@@ -0,0 +1,13 @@
package org.lucares.pdb.api;
public class JobAborter {
private boolean isCancelled = false;
public void cancel() {
isCancelled = true;
}
public boolean isCancelled() {
return isCancelled;
}
}