Advertisement
aironman

multithreading java sample

Feb 12th, 2019
195
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // cores can be four in my case because i have an i7 hyperthread with four physical cores.
  2. // numThreadsToRun can be as much as you can. I usually run this code with this value set up to 10000000
  3. // bigger the number, more time you need to finish the main task.
  4.  
  5. ExecutorService executor = Executors.newFixedThreadPool(cores * 2);
  6.  
  7. for (int i = 1; i <= numThreadsToRun; i++) {
  8. if (isEmExp) {
  9. Future<?> future = executor.submit(Utils.showSorteredValuesReversedOrder(pathToEM));
  10. future.get(5, TimeUnit.SECONDS);
  11. }
  12.  
  13. else if (isEm) {
  14. Future<?> future = executor.submit(Utils.calculateRandomEM(pathToEM));
  15. future.get(5, TimeUnit.SECONDS);
  16. } else if (isPrimitiva) {
  17. Future<?> future = executor.submit(Utils.calculatePrimitiva(pathToPrimitiva));
  18. future.get(5, TimeUnit.SECONDS);
  19. }
  20.  
  21. else if (all) {
  22. Future<?> futureRandomEM = executor.submit(Utils.calculateRandomEM(pathToEM));
  23. Future<?> futurePrimitive = executor.submit(Utils.calculatePrimitiva(pathToPrimitiva));
  24. futureRandomEM.get(5, TimeUnit.SECONDS);
  25. futurePrimitive.get(5, TimeUnit.SECONDS);
  26. } else if (allWithExperimental) {
  27. Future<?> futureSortered = executor.submit(Utils.showSorteredValuesReversedOrder(pathToEM));
  28. Future<?> futurePrimitive = executor.submit(Utils.calculatePrimitiva(pathToPrimitiva));
  29. futureSortered.get(5, TimeUnit.SECONDS);
  30. futurePrimitive.get(5, TimeUnit.SECONDS);
  31. }
  32. }
  33.  
  34. shutdownThreads(executor);
  35.  
  36. private static void shutdownThreads(ExecutorService executor) {
  37. try {
  38. System.out.println("attempt to shutdown executor");
  39. executor.shutdown();
  40. executor.awaitTermination(5, TimeUnit.MINUTES);
  41. } catch (InterruptedException e) {
  42. System.err.println("tasks interrupted");
  43. System.exit(-1);
  44. } finally {
  45. if (!executor.isTerminated()) {
  46. System.err.println("cancel non-finished tasks");
  47. }
  48. executor.shutdownNow();
  49. System.out.println("shutdown finished");
  50. }
  51. }
  52.  
  53. ...
  54. // Utils class, i only show one method...
  55. static Runnable calculatePrimitiva(String pathToPrimitiva) {
  56. Runnable runnable = () -> {
  57. try {
  58. List<EMPojo> myListEMPojo = Utils.processInputFile(pathToPrimitiva);
  59. final Comparator<EMPojo> compTotal2017 = (p1, p2) -> Integer.compare(p1.getTotal_2017(),
  60. p2.getTotal_2017());
  61. final Comparator<EMPojo> compTotal2018 = (p1, p2) -> Integer.compare(p1.getTotal_2018(),
  62. p2.getTotal_2018());
  63. long maxSize = 7l;
  64.  
  65. System.out.println("Primitiva 2017. ");
  66. myListEMPojo.stream().sorted(compTotal2017.reversed()) // sort from max to min
  67. .limit(maxSize).forEach(e -> System.out.println(e.toString()));
  68.  
  69. System.out.println("Primitiva 2018. ");
  70. myListEMPojo.stream().sorted(compTotal2018.reversed()) // sort from max to min
  71. .limit(maxSize).forEach(e -> System.out.println(e.toString()));
  72.  
  73. } catch (FileNotFoundException e1) {
  74. // TODO Auto-generated catch block
  75. e1.printStackTrace();
  76. }
  77. };
  78. return runnable;
  79. }
Advertisement
Advertisement
Advertisement
RAW Paste Data Copied
Advertisement