Guest User

Untitled

a guest
Feb 21st, 2018
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.64 KB | None | 0 0
  1. // want to parallelize this for loop
  2. for (Task task : tasks) {
  3. if (task.getCategories().isEmpty() || task.getEventList() == null
  4. || task.getMetaInfo() == null) {
  5. continue;
  6. }
  7. String itemId = task.getEventList().getId();
  8. String categoryId = task.getCategories().get(0).getId();
  9. Processor fp = new Processor(siteId, itemId, categoryId, poolType);
  10. Map<String, Integer> holder = fp.getDataHolder();
  11. if (!holder.isEmpty()) {
  12. for (Map.Entry<String, Integer> entry : holder.entrySet()) {
  13. info.putIfAbsent(entry.getKey(), entry.getValue());
  14. }
  15. List<Integer> values = new ArrayList<>();
  16. for (String key : holder.keySet()) {
  17. values.add(info.get(key));
  18. }
  19. itemToNumberMapping.put(itemId, StringUtils.join(values, ","));
  20. catToValueHolder.put(categoryId, StringUtils.join(values, ","));
  21. }
  22. Category cat = getCategory(task, holder.isEmpty());
  23. dealsByLeafCategory.add(cat);
  24. LinkedList<String> ids = getCategoryIds(task);
  25. catHolder.put(categoryId, ids.getLast());
  26. itemIds.add(itemId);
  27. }
  28.  
  29. private final ExecutorService service = Executors.newFixedThreadPool(10);
  30.  
  31. List<Future<Output>> futures = new ArrayList<Future<Output>>();
  32. for (final Input input : inputs) {
  33. Callable<Output> callable = new Callable<Output>() {
  34. public Output call() throws Exception {
  35. Output output = new Output();
  36. // process your input here and compute the output
  37. return output;
  38. }
  39. };
  40. futures.add(service.submit(callable));
  41. }
  42.  
  43. service.shutdown();
  44.  
  45. List<Output> outputs = new ArrayList<Output>();
  46. for (Future<Output> future : futures) {
  47. outputs.add(future.get());
  48. }
Add Comment
Please, Sign In to add comment