fork(1) download
  1. import java.util.concurrent.ExecutorService;
  2. import java.util.concurrent.Executors;
  3. import java.util.concurrent.TimeUnit;
  4.  
  5.  
  6. public class ParallelImplementation {
  7. private int numberOfCells;
  8. private double[] h0;
  9. private double[] h1;
  10. private double[] h2;
  11. private double[] h3;
  12. private double[] h4;
  13. private double[] h5;
  14. private double[] h6;
  15. private double[] h7;
  16. private double[] h8;
  17. private double[] h9;
  18.  
  19. public ParallelImplementation(int numberOfCells) {
  20. this.numberOfCells = numberOfCells;
  21. this.h0 = new double[numberOfCells];
  22. this.h1 = new double[numberOfCells];
  23. this.h2 = new double[numberOfCells];
  24. this.h3 = new double[numberOfCells];
  25. this.h4 = new double[numberOfCells];
  26. this.h5 = new double[numberOfCells];
  27. this.h6 = new double[numberOfCells];
  28. this.h7 = new double[numberOfCells];
  29. this.h8 = new double[numberOfCells];
  30. this.h9 = new double[numberOfCells];
  31. }
  32.  
  33. public void update() {
  34. final int numberOfThreads = Runtime.getRuntime().availableProcessors();
  35.  
  36. ExecutorService exec = Executors.newFixedThreadPool(numberOfThreads);
  37.  
  38. for(int thread = 0; thread < numberOfThreads; thread++) {
  39. final int threadId = thread;
  40. exec.submit(new Runnable() {
  41. @Override
  42. public void run() {
  43. for(int i = threadId; i < numberOfCells; i += numberOfThreads) {
  44. h0[i] = h0[i] + 1;
  45. h1[i] = h1[i] + 1;
  46. h2[i] = h2[i] + 1;
  47. h3[i] = h3[i] + 1;
  48. h4[i] = h4[i] + 1;
  49. h5[i] = h5[i] + 1;
  50. h6[i] = h6[i] + 1;
  51. h7[i] = h7[i] + 1;
  52. h8[i] = h8[i] + 1;
  53. h9[i] = h9[i] + 1;
  54. }
  55. }
  56. });
  57. }
  58.  
  59. exec.shutdown();
  60. try {
  61. exec.awaitTermination(Integer.MAX_VALUE, TimeUnit.SECONDS);
  62. } catch (InterruptedException e) {
  63. e.printStackTrace();
  64. }
  65. }
  66.  
  67. public static void main(String[] args) {
  68.  
  69. ParallelImplementation si = new ParallelImplementation(100000);
  70.  
  71. long start = System.currentTimeMillis();
  72.  
  73. for (int i = 0; i < 10000; i++) {
  74. if(i % 1000 == 0) {
  75. System.out.println(i);
  76. }
  77. si.update();
  78. }
  79.  
  80. long stop = System.currentTimeMillis();
  81. System.out.println("Time: " + (stop - start));
  82. }
  83.  
  84. }
  85.  
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.cpp:41:17: error: stray '@' in program
                 @Override
                 ^
prog.cpp:1:1: error: 'import' does not name a type
 import java.util.concurrent.ExecutorService;
 ^
prog.cpp:2:1: error: 'import' does not name a type
 import java.util.concurrent.Executors;
 ^
prog.cpp:3:1: error: 'import' does not name a type
 import java.util.concurrent.TimeUnit;
 ^
prog.cpp:6:1: error: expected unqualified-id before 'public'
 public class ParallelImplementation {
 ^
stdout
Standard output is empty