Run tasks directly instead of using the sync method

Fixes #1291
This commit is contained in:
TuxCoding
2025-05-13 17:05:23 +02:00
parent 8be0de4781
commit 1b45b8d2be
3 changed files with 23 additions and 61 deletions

View File

@ -43,9 +43,30 @@ public abstract class AbstractAsyncScheduler {
this.processingPool = processingPool;
}
public abstract CompletableFuture<Void> runAsync(Runnable task);
public CompletableFuture<Void> runAsync(Runnable task) {
return CompletableFuture.runAsync(() -> process(task), processingPool).exceptionally(error -> {
logger.warn("Error occurred on thread pool", error);
return null;
});
}
public abstract CompletableFuture<Void> runAsyncDelayed(Runnable task, Duration delay);
public CompletableFuture<Void> runAsyncDelayed(Runnable task, Duration delay) {
return CompletableFuture.runAsync(() -> {
currentlyRunning.incrementAndGet();
try {
Thread.sleep(delay.toMillis());
task.run();
} catch (InterruptedException interruptedException) {
// restore interrupt flag
Thread.currentThread().interrupt();
} finally {
currentlyRunning.getAndDecrement();
}
}, processingPool).exceptionally(error -> {
logger.warn("Error occurred on thread pool", error);
return null;
});
}
protected void process(Runnable task) {
currentlyRunning.incrementAndGet();

View File

@ -27,8 +27,6 @@ package com.github.games647.fastlogin.core.scheduler;
import org.slf4j.Logger;
import java.time.Duration;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.Executor;
/**
@ -46,30 +44,4 @@ public class AsyncScheduler extends AbstractAsyncScheduler {
+ "Upgrade Java to 21+ for improved performance");
}
@Override
public CompletableFuture<Void> runAsync(Runnable task) {
return CompletableFuture.runAsync(() -> process(task), processingPool).exceptionally(error -> {
logger.warn("Error occurred on thread pool", error);
return null;
});
}
@Override
public CompletableFuture<Void> runAsyncDelayed(Runnable task, Duration delay) {
return CompletableFuture.runAsync(() -> {
currentlyRunning.incrementAndGet();
try {
Thread.sleep(delay.toMillis());
process(task);
} catch (InterruptedException interruptedException) {
// restore interrupt flag
Thread.currentThread().interrupt();
} finally {
currentlyRunning.getAndDecrement();
}
}, processingPool).exceptionally(error -> {
logger.warn("Error occurred on thread pool", error);
return null;
});
}
}

View File

@ -27,8 +27,6 @@ package com.github.games647.fastlogin.core.scheduler;
import org.slf4j.Logger;
import java.time.Duration;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.Executor;
import java.util.concurrent.Executors;
@ -46,33 +44,4 @@ public class AsyncScheduler extends AbstractAsyncScheduler {
logger.info("Using optimized green threads with Java 21");
}
@Override
public CompletableFuture<Void> runAsync(Runnable task) {
return CompletableFuture
.runAsync(() -> process(task), processingPool)
.exceptionally(error -> {
logger.warn("Error occurred on thread pool", error);
return null;
});
}
@Override
public CompletableFuture<Void> runAsyncDelayed(Runnable task, Duration delay) {
return CompletableFuture.runAsync(() -> {
currentlyRunning.incrementAndGet();
try {
Thread.sleep(delay);
process(task);
} catch (InterruptedException interruptedException) {
// restore interrupt flag
Thread.currentThread().interrupt();
} finally {
currentlyRunning.getAndDecrement();
}
}, processingPool).exceptionally(error -> {
logger.warn("Error occurred on thread pool", error);
return null;
});
}
}