
devutility.internal.util.concurrent.CompletionServiceUtils Maven / Gradle / Ivy
package devutility.internal.util.concurrent;
import java.util.List;
import java.util.concurrent.CompletionService;
import java.util.concurrent.ExecutorCompletionService;
import java.util.concurrent.ExecutorService;
public class CompletionServiceUtils {
/**
* Create a CompletionService instance
* @return {@code CompletionService}
*/
public static CompletionService instance() {
ExecutorService executorService = ExecutorServiceUtils.threadPoolExecutor();
return new ExecutorCompletionService<>(executorService);
}
/**
* Run a list of Runnable object
* @param runnables: Runnable list
* @throws InterruptedException
*/
public static void run(List runnables) throws InterruptedException {
if (runnables == null) {
return;
}
CompletionService completionService = instance();
for (Runnable runnable : runnables) {
completionService.submit(runnable, null);
}
int index = 0;
while (index < runnables.size()) {
completionService.take();
index++;
}
ExecutorServiceUtils.threadPoolExecutor().shutdown();
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy