Skip to content

Commit 31e04bb

Browse files
committed
Fixed Processing sometimes running processors on the main server thread
1 parent 38084be commit 31e04bb

File tree

1 file changed

+30
-24
lines changed

1 file changed

+30
-24
lines changed

Plan/src/main/java/com/djrapitops/plan/system/processing/Processing.java

Lines changed: 30 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -38,16 +38,22 @@ public static void saveInstance(Object obj) {
3838

3939
public static void submitNonCritical(Runnable runnable) {
4040
saveInstance(runnable);
41-
CompletableFuture.supplyAsync(() -> runnable, getInstance().nonCriticalExecutor)
42-
.thenAccept(Runnable::run)
43-
.handle(Processing::exceptionHandler);
41+
ExecutorService executor = getInstance().nonCriticalExecutor;
42+
if (executor.isShutdown()) {
43+
return;
44+
}
45+
CompletableFuture.supplyAsync(() -> {
46+
runnable.run();
47+
return true;
48+
}, executor).handle(Processing::exceptionHandler);
4449
}
4550

4651
public static void submitCritical(Runnable runnable) {
4752
saveInstance(runnable);
48-
CompletableFuture.supplyAsync(() -> runnable, getInstance().criticalExecutor)
49-
.thenAccept(Runnable::run)
50-
.handle(Processing::exceptionHandler);
53+
CompletableFuture.supplyAsync(() -> {
54+
runnable.run();
55+
return true;
56+
}, getInstance().criticalExecutor).handle(Processing::exceptionHandler);
5157
}
5258

5359
public static void submitNonCritical(Runnable... runnables) {
@@ -72,15 +78,17 @@ public static <T> Future<T> submit(Callable<T> task) {
7278

7379
public static <T> Future<T> submitNonCritical(Callable<T> task) {
7480
saveInstance(task);
75-
return CompletableFuture.supplyAsync(() -> task, getInstance().nonCriticalExecutor)
76-
.thenApply(tCallable -> {
77-
try {
78-
return tCallable.call();
79-
} catch (Exception e) {
80-
throw new IllegalStateException(e);
81-
}
82-
})
83-
.handle(Processing::exceptionHandler);
81+
ExecutorService executor = getInstance().nonCriticalExecutor;
82+
if (executor.isShutdown()) {
83+
return null;
84+
}
85+
return CompletableFuture.supplyAsync(() -> {
86+
try {
87+
return task.call();
88+
} catch (Exception e) {
89+
throw new IllegalStateException(e);
90+
}
91+
}, getInstance().nonCriticalExecutor).handle(Processing::exceptionHandler);
8492
}
8593

8694
private static <T> T exceptionHandler(T t, Throwable throwable) {
@@ -92,15 +100,13 @@ private static <T> T exceptionHandler(T t, Throwable throwable) {
92100

93101
public static <T> Future<T> submitCritical(Callable<T> task) {
94102
saveInstance(task);
95-
return CompletableFuture.supplyAsync(() -> task, getInstance().criticalExecutor)
96-
.thenApply(tCallable -> {
97-
try {
98-
return tCallable.call();
99-
} catch (Exception e) {
100-
throw new IllegalStateException(e);
101-
}
102-
})
103-
.handle(Processing::exceptionHandler);
103+
return CompletableFuture.supplyAsync(() -> {
104+
try {
105+
return task.call();
106+
} catch (Exception e) {
107+
throw new IllegalStateException(e);
108+
}
109+
}, getInstance().criticalExecutor).handle(Processing::exceptionHandler);
104110
}
105111

106112
public static Processing getInstance() {

0 commit comments

Comments
 (0)