All Downloads are FREE. Search and download functionalities are using the official Maven repository.

devutility.internal.util.concurrent.CompletionServiceUtils Maven / Gradle / Ivy

There is a newer version: 1.3.8.1
Show newest version
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);
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy