Here you can find the source of runConcurrently(final Callable
Parameter | Description |
---|---|
task | a Callable to run concurrently. |
Parameter | Description |
---|---|
Exception | if any of the executed tasks fails. |
public static void runConcurrently(final Callable<Void> task) throws Exception
//package com.java2s; import java.util.ArrayList; import java.util.List; import java.util.concurrent.Callable; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.concurrent.Future; public class Main { /**/* www . ja va2s. c o m*/ * Runs a task concurrently. Allows to test thread-safe behavior. * * @param task a {@link Callable} to run concurrently. * @throws Exception if any of the executed tasks fails. */ public static void runConcurrently(final Callable<Void> task) throws Exception { final ExecutorService service = Executors.newFixedThreadPool(5); final List<Future<?>> futures = new ArrayList<Future<?>>(); for (int i = 0; i < 10; i++) { futures.add(service.submit(task)); } for (final Future<?> future : futures) { future.get(); } } }