From 31e04bb0807da13c743cd3f657df1284d67b03a6 Mon Sep 17 00:00:00 2001 From: Rsl1122 Date: Thu, 19 Jul 2018 14:11:16 +0300 Subject: [PATCH] Fixed Processing sometimes running processors on the main server thread --- .../plan/system/processing/Processing.java | 54 ++++++++++--------- 1 file changed, 30 insertions(+), 24 deletions(-) diff --git a/Plan/src/main/java/com/djrapitops/plan/system/processing/Processing.java b/Plan/src/main/java/com/djrapitops/plan/system/processing/Processing.java index 21c8bf6aa9..edc5cef7a1 100644 --- a/Plan/src/main/java/com/djrapitops/plan/system/processing/Processing.java +++ b/Plan/src/main/java/com/djrapitops/plan/system/processing/Processing.java @@ -38,16 +38,22 @@ public static void saveInstance(Object obj) { public static void submitNonCritical(Runnable runnable) { saveInstance(runnable); - CompletableFuture.supplyAsync(() -> runnable, getInstance().nonCriticalExecutor) - .thenAccept(Runnable::run) - .handle(Processing::exceptionHandler); + ExecutorService executor = getInstance().nonCriticalExecutor; + if (executor.isShutdown()) { + return; + } + CompletableFuture.supplyAsync(() -> { + runnable.run(); + return true; + }, executor).handle(Processing::exceptionHandler); } public static void submitCritical(Runnable runnable) { saveInstance(runnable); - CompletableFuture.supplyAsync(() -> runnable, getInstance().criticalExecutor) - .thenAccept(Runnable::run) - .handle(Processing::exceptionHandler); + CompletableFuture.supplyAsync(() -> { + runnable.run(); + return true; + }, getInstance().criticalExecutor).handle(Processing::exceptionHandler); } public static void submitNonCritical(Runnable... runnables) { @@ -72,15 +78,17 @@ public static Future submit(Callable task) { public static Future submitNonCritical(Callable task) { saveInstance(task); - return CompletableFuture.supplyAsync(() -> task, getInstance().nonCriticalExecutor) - .thenApply(tCallable -> { - try { - return tCallable.call(); - } catch (Exception e) { - throw new IllegalStateException(e); - } - }) - .handle(Processing::exceptionHandler); + ExecutorService executor = getInstance().nonCriticalExecutor; + if (executor.isShutdown()) { + return null; + } + return CompletableFuture.supplyAsync(() -> { + try { + return task.call(); + } catch (Exception e) { + throw new IllegalStateException(e); + } + }, getInstance().nonCriticalExecutor).handle(Processing::exceptionHandler); } private static T exceptionHandler(T t, Throwable throwable) { @@ -92,15 +100,13 @@ private static T exceptionHandler(T t, Throwable throwable) { public static Future submitCritical(Callable task) { saveInstance(task); - return CompletableFuture.supplyAsync(() -> task, getInstance().criticalExecutor) - .thenApply(tCallable -> { - try { - return tCallable.call(); - } catch (Exception e) { - throw new IllegalStateException(e); - } - }) - .handle(Processing::exceptionHandler); + return CompletableFuture.supplyAsync(() -> { + try { + return task.call(); + } catch (Exception e) { + throw new IllegalStateException(e); + } + }, getInstance().criticalExecutor).handle(Processing::exceptionHandler); } public static Processing getInstance() {