Guest User

Untitled

a guest
Jan 24th, 2019
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.31 KB | None | 0 0
  1. public class Problem {
  2. private static final int MY_THREADS = 10;
  3.  
  4.  
  5. public static void main(String[] args) {
  6. double[] array = {...};
  7.  
  8. double[] maximums = new double[3];
  9.  
  10. for (int i = 0; i < maximums.length; ++i) {
  11. maximums[i] = Double.MIN_VALUE;
  12. }
  13.  
  14. ExecutorService executor = Executors.newFixedThreadPool(MY_THREADS);
  15.  
  16. Runnable worker = new MyRunnable(array, maximums);
  17. executor.execute(worker);
  18. executor.shutdown();
  19.  
  20. while (!executor.isTerminated()) {
  21.  
  22. }
  23. System.out.println(Arrays.toString(maximums));
  24. }
  25.  
  26. public static class MyRunnable implements Runnable {
  27. private double[] array;
  28.  
  29. private double[] maximums;
  30.  
  31. MyRunnable(double[] array, double[] maximums) {
  32. this.array = array;
  33. this.maximums = maximums;
  34. }
  35.  
  36. @Override
  37. public void run() {
  38.  
  39. int i = 0;
  40. while (i < array.length) {
  41. if (array[i] > maximums[getMinIndex(maximums)]) {
  42. maximums[getMinIndex(maximums)] = array[i];
  43. }
  44.  
  45. ++i;
  46. }
  47. }
  48. }
  49.  
  50. private static int getMinIndex(double[] array) {
  51. int minIndex = -1;
  52. double min = Double.MAX_VALUE;
  53.  
  54. for (int i = 0; i < array.length; ++i) {
  55. if (array[i] < min) {
  56. min = array[i];
  57. minIndex = i;
  58. }
  59. }
  60. return minIndex;
  61. }
Add Comment
Please, Sign In to add comment