BAEL-58616 Code for the Check If All Runnable Are Done article

This commit is contained in:
thibault.faure 2022-10-13 01:38:18 +02:00
parent 1f1d49bcb1
commit 43ba5e80a4
2 changed files with 97 additions and 0 deletions

View File

@ -0,0 +1,46 @@
package com.baeldung.donerunnables;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class RunnableCompletionCheckerWithCompletableFuture {
private static final Logger LOGGER = LoggerFactory.getLogger(RunnableCompletionCheckerWithCompletableFuture.class);
private static final int NUMBER_OF_RUNNABLES = 5;
private static final int PAUSE_MILLIS = 1000;
private static Runnable RUNNABLE = () -> {
try {
LOGGER.info("launching runnable");
Thread.sleep(PAUSE_MILLIS);
} catch (InterruptedException e) {
}
};
public static void main(String args[]) throws InterruptedException {
List<Runnable> runnables = IntStream.range(0, NUMBER_OF_RUNNABLES)
.mapToObj(x -> RUNNABLE)
.collect(Collectors.toList());
CompletableFuture<?>[] completableFutures = runAsynchronousTasks(runnables);
LOGGER.info("Right after the creation of the completable future array, every completable future is done: {}", isEveryCompletableFutureDone(completableFutures));
Thread.sleep((NUMBER_OF_RUNNABLES + 1) * PAUSE_MILLIS);
LOGGER.info("After {} seconds, every completable future is done: {}", NUMBER_OF_RUNNABLES + 1, isEveryCompletableFutureDone(completableFutures));
}
public static CompletableFuture<?>[] runAsynchronousTasks(List<Runnable> runnables) {
return runnables.stream()
.map(CompletableFuture::runAsync)
.toArray(CompletableFuture<?>[]::new);
}
public static boolean isEveryCompletableFutureDone(CompletableFuture<?>[] completableFutures) {
return CompletableFuture.allOf(completableFutures)
.isDone();
}
}

View File

@ -0,0 +1,51 @@
package com.baeldung.donerunnables;
import java.util.List;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class RunnableCompletionCheckerWithThreadPoolExecutor {
private static final Logger LOGGER = LoggerFactory.getLogger(RunnableCompletionCheckerWithCompletableFuture.class);
private static final int NUMBER_OF_RUNNABLES = 5;
private static final int PAUSE_MILLIS = 1000;
private static final int NUMBER_OF_THREADS = 5;
private static Runnable RUNNABLE = () -> {
try {
LOGGER.info("launching runnable");
Thread.sleep(PAUSE_MILLIS);
} catch (InterruptedException e) {
}
};
public static void main(String args[]) throws InterruptedException {
List<Runnable> runnables = IntStream.range(0, NUMBER_OF_RUNNABLES)
.mapToObj(x -> RUNNABLE)
.collect(Collectors.toList());
ThreadPoolExecutor executor = createThreadPoolExecutor(runnables);
executor.shutdown();
LOGGER.info("After a timeout of 0 seconds, every Runnable is done: {}", isEveryRunnableDone(executor, 0));
Thread.sleep(100);
LOGGER.info("After a timeout of 100 milliseconds, every Runnable is done: {}", isEveryRunnableDone(executor, 100));
Thread.sleep(2000);
LOGGER.info("After a timeout of 2 seconds, every Runnable is done: {}", isEveryRunnableDone(executor, 1500));
}
public static ThreadPoolExecutor createThreadPoolExecutor(List<Runnable> runnables) {
ThreadPoolExecutor executor = (ThreadPoolExecutor) Executors.newFixedThreadPool(NUMBER_OF_THREADS);
runnables.forEach(executor::execute);
return executor;
}
public static boolean isEveryRunnableDone(ThreadPoolExecutor executor, int timeout) throws InterruptedException {
return executor.awaitTermination(timeout, TimeUnit.MILLISECONDS);
}
}