BAEL-6579: completable future's thread pool

This commit is contained in:
emanueltrandafir1993 2023-07-01 13:24:01 +02:00
parent dad0e8cf5e
commit dbbd2a0a12
2 changed files with 110 additions and 0 deletions

View File

@ -0,0 +1,28 @@
package com.baeldung.concurrent.completablefuture.threadpool;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.Executor;
import java.util.concurrent.Executors;
import java.util.function.Supplier;
public class CustomCompletableFuture<T> extends CompletableFuture<T> {
private static final Executor executor = Executors.newSingleThreadExecutor(runnable -> new Thread(runnable, "Custom-Single-Thread"));
public static <TYPE> CustomCompletableFuture<TYPE> supplyAsync(Supplier<TYPE> supplier) {
CustomCompletableFuture<TYPE> future = new CustomCompletableFuture<>();
executor.execute(() -> {
try {
future.complete(supplier.get());
} catch (Exception ex) {
future.completeExceptionally(ex);
}
});
return future;
}
@Override
public Executor defaultExecutor() {
return executor;
}
}

View File

@ -0,0 +1,82 @@
package com.baeldung.concurrent.completablefuture.threadpool;
import static org.assertj.core.api.Assertions.assertThat;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.Executor;
import java.util.concurrent.Executors;
import org.junit.jupiter.api.Test;
public class CompletableFutureThreadPoolUnitTest {
@Test
void whenUsingNonAsync_thenUsesMainThread() {
CompletableFuture<String> name = CompletableFuture.supplyAsync(() -> "Baeldung");
CompletableFuture<Integer> nameLength = name.thenApply(value -> {
printCurrentThread();
return value.length();
});
assertThat(nameLength).isCompletedWithValue(8);
}
@Test
void whenUsingNonAsync_thenUsesCallersThread() throws InterruptedException {
Runnable test = () -> {
CompletableFuture<String> name = CompletableFuture.supplyAsync(() -> "Baeldung");
CompletableFuture<Integer> nameLength = name.thenApply(value -> {
printCurrentThread();
return value.length();
});
assertThat(nameLength).isCompletedWithValue(8);
};
new Thread(test, "test-thread").start();
Thread.sleep(100l);
}
@Test
void whenUsingAsync_thenUsesCommonPool() {
CompletableFuture<String> name = CompletableFuture.supplyAsync(() -> "Baeldung");
CompletableFuture<Integer> nameLength = name.thenApplyAsync(value -> {
printCurrentThread();
return value.length();
});
assertThat(nameLength).isCompletedWithValue(8);
}
@Test
void whenUsingAsync_thenUsesCustomExecutor() {
Executor testExecutor = Executors.newFixedThreadPool(5);
CompletableFuture<String> name = CompletableFuture.supplyAsync(() -> "Baeldung");
CompletableFuture<Integer> nameLength = name.thenApplyAsync(value -> {
printCurrentThread();
return value.length();
}, testExecutor);
assertThat(nameLength).isCompletedWithValue(8);
}
@Test
void whenOverridingDefaultThreadPool_thenUsesCustomExecutor() {
CompletableFuture<String> name = CustomCompletableFuture.supplyAsync(() -> "Baeldung");
CompletableFuture<Integer> nameLength = name.thenApplyAsync(value -> {
printCurrentThread();
return value.length();
});
assertThat(nameLength).isCompletedWithValue(8);
}
private static void printCurrentThread() {
System.out.println(Thread.currentThread().getName());
}
}