Created
February 10, 2016 10:38
-
-
Save dborovikov/956a890bdd3094937678 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import com.google.common.collect.Lists; | |
| import java.util.ArrayList; | |
| import java.util.List; | |
| import java.util.concurrent.ExecutionException; | |
| import java.util.concurrent.ExecutorService; | |
| import java.util.concurrent.Executors; | |
| import java.util.concurrent.Future; | |
| public class ParallelGroups { | |
| public static final int CONCURRENCY = 5; | |
| private static double[] compute(double[] a, double[] w) { | |
| return a; | |
| } | |
| public static void main(String[] args) throws ExecutionException, InterruptedException { | |
| final ExecutorService executor = Executors.newCachedThreadPool(); | |
| final List<double[]> input = new ArrayList<>(); | |
| final double[] w = new double[0]; | |
| for (List<double[]> group : Lists.partition(input, CONCURRENCY)) { | |
| List<Future<double[]>> futures = Lists.transform(group, elem -> executor.submit(() -> compute(elem, w))); | |
| for (Future<double[]> future : futures) { | |
| double[] result = future.get(); | |
| for (int i = 0; i < result.length; i++) { | |
| w[i] += result[i]; | |
| } | |
| } | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment