Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace Dispatcher's internal ExecutorService with an inline virtual thread #877

Merged
merged 1 commit into from
May 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 1 addition & 6 deletions src/main/java/com/pivovarit/collectors/Dispatcher.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,8 @@ final class Dispatcher<T> {
private static final Runnable POISON_PILL = () -> System.out.println("Why so serious?");

private final CompletableFuture<Void> completionSignaller = new CompletableFuture<>();

private final BlockingQueue<Runnable> workingQueue = new LinkedBlockingQueue<>();

private final ExecutorService dispatcher = Executors.newVirtualThreadPerTaskExecutor();
private final Executor executor;
private final Semaphore limiter;

Expand Down Expand Up @@ -53,7 +51,7 @@ static <T> Dispatcher<T> virtual() {

void start() {
if (!started.getAndSet(true)) {
dispatcher.execute(() -> {
Thread.ofVirtual().start(() -> {
try {
while (true) {
try {
Expand Down Expand Up @@ -90,8 +88,6 @@ void stop() {
workingQueue.put(POISON_PILL);
} catch (InterruptedException e) {
completionSignaller.completeExceptionally(e);
} finally {
dispatcher.shutdown();
}
}

Expand Down Expand Up @@ -123,7 +119,6 @@ private FutureTask<Void> completionTask(Supplier<T> supplier, InterruptibleCompl
private void handle(Throwable e) {
shortCircuited = true;
completionSignaller.completeExceptionally(e);
dispatcher.shutdownNow();
}

private static Function<Throwable, Void> shortcircuit(InterruptibleCompletableFuture<?> future) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ final class FutureCollectors {
.collect(collector));

for (var f : list) {
f.whenComplete((t, throwable) -> {
f.whenComplete((__, throwable) -> {
if (throwable != null) {
future.completeExceptionally(throwable);
}
Expand Down