Advertisement
Guest User

Untitled

a guest
Feb 10th, 2016
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.10 KB | None | 0 0
  1. import com.google.common.collect.Lists;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.List;
  5. import java.util.concurrent.ExecutionException;
  6. import java.util.concurrent.ExecutorService;
  7. import java.util.concurrent.Executors;
  8. import java.util.concurrent.Future;
  9.  
  10. public class ParallelGroups {
  11. public static final int CONCURRENCY = 5;
  12.  
  13. private static double[] compute(double[] a, double[] w) {
  14. return a;
  15. }
  16.  
  17. public static void main(String[] args) throws ExecutionException, InterruptedException {
  18. final ExecutorService executor = Executors.newCachedThreadPool();
  19. final List<double[]> input = new ArrayList<>();
  20. final double[] w = new double[0];
  21. for (List<double[]> group : Lists.partition(input, CONCURRENCY)) {
  22. List<Future<double[]>> futures = Lists.transform(group, elem -> executor.submit(() -> compute(elem, w)));
  23. for (Future<double[]> future : futures) {
  24. double[] result = future.get();
  25. for (int i = 0; i < result.length; i++) {
  26. w[i] += result[i];
  27. }
  28. }
  29. }
  30. }
  31. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement