Advertisement
Guest User

Untitled

a guest
Jun 25th, 2019
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.93 KB | None | 0 0
  1. package concurrent;
  2. import java.util.concurrent.*;
  3.  
  4. import concurrent.multi.Mult;
  5.  
  6. public class matrix {
  7.  
  8. public static void main(String[] args) {
  9. // TODO Auto-generated method stub
  10. int[] a = {4, 4, 5, 1, 8, 10, 15, 1, 6, 8 };
  11. int[] b = {2, 7, 5, 4, 8, 12, 13, 1, 7, 8 };
  12.  
  13. // multiply these two matricies
  14. //int result = a[0]*b[0] + a[1][b]; // up to a[9]*b[9]
  15. int result = 0;
  16. for (int i=0; i<a.length; i++)
  17. result = result + a[i]*b[i];
  18. System.out.printf("Result %d%n", result);
  19.  
  20. // create 10 Mult objects to run as threads
  21. Mult m1 = new Mult(a[0],b[0]); //where error occurs
  22. Mult m2 = new Mult(a[1],b[1]);
  23. Mult m3 = new Mult(a[2],b[2]);
  24. Mult m4 = new Mult(a[3],b[3]);
  25. Mult m5 = new Mult(a[4],b[4]);
  26. Mult m6 = new Mult(a[5],b[5]);
  27. Mult m7 = new Mult(a[6],b[6]);
  28. Mult m8 = new Mult(a[7],b[7]);
  29. Mult m9 = new Mult(a[8],b[8]);
  30. Mult m10 = new Mult(a[9],b[9]);
  31. // you do the rest here
  32.  
  33. // creates a thread pool
  34. ExecutorService executorService = Executors.newCachedThreadPool();
  35.  
  36. executorService.execute(m1);
  37. executorService.execute(m2);
  38. executorService.execute(m3);
  39. executorService.execute(m4);
  40. executorService.execute(m5);
  41. executorService.execute(m6);
  42. executorService.execute(m7);
  43. executorService.execute(m8);
  44. executorService.execute(m9);
  45. executorService.execute(m10);
  46. // you do the rest here
  47.  
  48. // when threads finish we have all the results
  49. // now add them together to produce the final answer
  50. result = m1.getResult() + m2.getResult(); // + you do the rest
  51.  
  52. executorService.shutdown();
  53.  
  54. }
  55. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement