Skip to content

Commit 1b45b8d

Browse files
committed
Run tasks directly instead of using the sync method
Fixes #1291
1 parent 8be0de4 commit 1b45b8d

File tree

3 files changed

+23
-61
lines changed

3 files changed

+23
-61
lines changed

core/src/main/java/com/github/games647/fastlogin/core/scheduler/AbstractAsyncScheduler.java

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,30 @@ public AbstractAsyncScheduler(Logger logger, Executor processingPool) {
4343
this.processingPool = processingPool;
4444
}
4545

46-
public abstract CompletableFuture<Void> runAsync(Runnable task);
46+
public CompletableFuture<Void> runAsync(Runnable task) {
47+
return CompletableFuture.runAsync(() -> process(task), processingPool).exceptionally(error -> {
48+
logger.warn("Error occurred on thread pool", error);
49+
return null;
50+
});
51+
}
4752

48-
public abstract CompletableFuture<Void> runAsyncDelayed(Runnable task, Duration delay);
53+
public CompletableFuture<Void> runAsyncDelayed(Runnable task, Duration delay) {
54+
return CompletableFuture.runAsync(() -> {
55+
currentlyRunning.incrementAndGet();
56+
try {
57+
Thread.sleep(delay.toMillis());
58+
task.run();
59+
} catch (InterruptedException interruptedException) {
60+
// restore interrupt flag
61+
Thread.currentThread().interrupt();
62+
} finally {
63+
currentlyRunning.getAndDecrement();
64+
}
65+
}, processingPool).exceptionally(error -> {
66+
logger.warn("Error occurred on thread pool", error);
67+
return null;
68+
});
69+
}
4970

5071
protected void process(Runnable task) {
5172
currentlyRunning.incrementAndGet();

core/src/main/java/com/github/games647/fastlogin/core/scheduler/AsyncScheduler.java

Lines changed: 0 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,6 @@
2727

2828
import org.slf4j.Logger;
2929

30-
import java.time.Duration;
31-
import java.util.concurrent.CompletableFuture;
3230
import java.util.concurrent.Executor;
3331

3432
/**
@@ -46,30 +44,4 @@ public AsyncScheduler(Logger logger, Executor processingPool) {
4644
+ "Upgrade Java to 21+ for improved performance");
4745
}
4846

49-
@Override
50-
public CompletableFuture<Void> runAsync(Runnable task) {
51-
return CompletableFuture.runAsync(() -> process(task), processingPool).exceptionally(error -> {
52-
logger.warn("Error occurred on thread pool", error);
53-
return null;
54-
});
55-
}
56-
57-
@Override
58-
public CompletableFuture<Void> runAsyncDelayed(Runnable task, Duration delay) {
59-
return CompletableFuture.runAsync(() -> {
60-
currentlyRunning.incrementAndGet();
61-
try {
62-
Thread.sleep(delay.toMillis());
63-
process(task);
64-
} catch (InterruptedException interruptedException) {
65-
// restore interrupt flag
66-
Thread.currentThread().interrupt();
67-
} finally {
68-
currentlyRunning.getAndDecrement();
69-
}
70-
}, processingPool).exceptionally(error -> {
71-
logger.warn("Error occurred on thread pool", error);
72-
return null;
73-
});
74-
}
7547
}

core/src/main/java21/com/github/games647/fastlogin/core/scheduler/AsyncScheduler.java

Lines changed: 0 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,6 @@
2727

2828
import org.slf4j.Logger;
2929

30-
import java.time.Duration;
31-
import java.util.concurrent.CompletableFuture;
3230
import java.util.concurrent.Executor;
3331
import java.util.concurrent.Executors;
3432

@@ -46,33 +44,4 @@ public AsyncScheduler(Logger logger, Executor processingPool) {
4644

4745
logger.info("Using optimized green threads with Java 21");
4846
}
49-
50-
@Override
51-
public CompletableFuture<Void> runAsync(Runnable task) {
52-
return CompletableFuture
53-
.runAsync(() -> process(task), processingPool)
54-
.exceptionally(error -> {
55-
logger.warn("Error occurred on thread pool", error);
56-
return null;
57-
});
58-
}
59-
60-
@Override
61-
public CompletableFuture<Void> runAsyncDelayed(Runnable task, Duration delay) {
62-
return CompletableFuture.runAsync(() -> {
63-
currentlyRunning.incrementAndGet();
64-
try {
65-
Thread.sleep(delay);
66-
process(task);
67-
} catch (InterruptedException interruptedException) {
68-
// restore interrupt flag
69-
Thread.currentThread().interrupt();
70-
} finally {
71-
currentlyRunning.getAndDecrement();
72-
}
73-
}, processingPool).exceptionally(error -> {
74-
logger.warn("Error occurred on thread pool", error);
75-
return null;
76-
});
77-
}
7847
}

0 commit comments

Comments
 (0)