devutility.internal.util.concurrent.CompletionServiceUtils Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of devutility.internal Show documentation
Show all versions of devutility.internal Show documentation
Some utilities for Java development
package devutility.internal.util.concurrent;
import java.util.List;
import java.util.concurrent.CompletionService;
import java.util.concurrent.ExecutorCompletionService;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.TimeUnit;
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();
ExecutorServiceUtils.threadPoolExecutor().awaitTermination(0, TimeUnit.SECONDS);
}
}