diff --git a/core/src/main/java/com/github/games647/fastlogin/core/scheduler/AbstractAsyncScheduler.java b/core/src/main/java/com/github/games647/fastlogin/core/scheduler/AbstractAsyncScheduler.java index 14218b9a..982302c5 100644 --- a/core/src/main/java/com/github/games647/fastlogin/core/scheduler/AbstractAsyncScheduler.java +++ b/core/src/main/java/com/github/games647/fastlogin/core/scheduler/AbstractAsyncScheduler.java @@ -43,9 +43,30 @@ public abstract class AbstractAsyncScheduler { this.processingPool = processingPool; } - public abstract CompletableFuture runAsync(Runnable task); + public CompletableFuture runAsync(Runnable task) { + return CompletableFuture.runAsync(() -> process(task), processingPool).exceptionally(error -> { + logger.warn("Error occurred on thread pool", error); + return null; + }); + } - public abstract CompletableFuture runAsyncDelayed(Runnable task, Duration delay); + public CompletableFuture 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(); diff --git a/core/src/main/java/com/github/games647/fastlogin/core/scheduler/AsyncScheduler.java b/core/src/main/java/com/github/games647/fastlogin/core/scheduler/AsyncScheduler.java index f438a84f..1836a339 100644 --- a/core/src/main/java/com/github/games647/fastlogin/core/scheduler/AsyncScheduler.java +++ b/core/src/main/java/com/github/games647/fastlogin/core/scheduler/AsyncScheduler.java @@ -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 runAsync(Runnable task) { - return CompletableFuture.runAsync(() -> process(task), processingPool).exceptionally(error -> { - logger.warn("Error occurred on thread pool", error); - return null; - }); - } - - @Override - public CompletableFuture 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; - }); - } } diff --git a/core/src/main/java21/com/github/games647/fastlogin/core/scheduler/AsyncScheduler.java b/core/src/main/java21/com/github/games647/fastlogin/core/scheduler/AsyncScheduler.java index e5cbb7b3..9e93d9fd 100644 --- a/core/src/main/java21/com/github/games647/fastlogin/core/scheduler/AsyncScheduler.java +++ b/core/src/main/java21/com/github/games647/fastlogin/core/scheduler/AsyncScheduler.java @@ -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 runAsync(Runnable task) { - return CompletableFuture - .runAsync(() -> process(task), processingPool) - .exceptionally(error -> { - logger.warn("Error occurred on thread pool", error); - return null; - }); - } - - @Override - public CompletableFuture 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; - }); - } }