Advertisement
SSemen

Untitled

Sep 15th, 2022
1,033
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.76 KB | None | 0 0
  1. package com.company;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.Random;
  5. import java.util.concurrent.*;
  6.  
  7. public class Main {
  8.  
  9.     static final int MAX = 100;
  10.     static Integer[] arr = new Integer[100000000];
  11.  
  12.     static Integer maximum = 10, minimum = 1000000;
  13.  
  14.     public static void main(String[] args) throws Exception {
  15.         LoadArr();
  16.         ImplementationThreadPoolExecutor(4);
  17.         ImplementationForkJoinPool();
  18.     }
  19.  
  20.     public static void LoadArr(){
  21.         for(int i = 0; i < MAX; i++){
  22.             Random rn = new Random();
  23.             int n = maximum - minimum + 1;
  24.             int a = rn.nextInt() % n;
  25.             int  randomNum =  minimum + a;
  26.  
  27.             arr[i] = randomNum;
  28.         }
  29.     }
  30.  
  31.     public static void ImplementationThreadPoolExecutor(Integer countThread) throws Exception {
  32.         ThreadPoolExecutor es = new ThreadPoolExecutor(
  33.                 20, 40, 1, TimeUnit.MILLISECONDS,
  34.                 new LinkedBlockingQueue<>(30), new MyReject()
  35.         );
  36.  
  37.  
  38.         ArrayList<Future<Integer>> subArr = new ArrayList<Future<Integer>>(100);
  39.  
  40.         for (int i = 0; i < countThread; i++){
  41.             Integer begin = (MAX / countThread) * i;
  42.             Integer end = (MAX / countThread) * (i + 1);
  43.             subArr.add(es.submit(new MyCallable(begin, end, arr)));
  44.         }
  45.  
  46.         Integer sum = 0;
  47.         for(int i = 0; i < countThread; i++){
  48.             sum += subArr.get(i).get();
  49.         }
  50.  
  51.         es.shutdown();
  52.     }
  53.  
  54.     public static void ImplementationForkJoinPool(){
  55.         ForkJoinPool pool = new ForkJoinPool(4);
  56.         System.out.println(pool.invoke(new MyRecursiveTask(0, MAX, arr)));
  57.     }
  58. }
  59.  
  60. class MyReject implements RejectedExecutionHandler{
  61.  
  62.     @Override
  63.     public void rejectedExecution(Runnable r, ThreadPoolExecutor executor) {
  64.         System.out.println("REJECTED");
  65.     }
  66. }
  67.  
  68. class MyCallable implements Callable<Integer>
  69.  {
  70.      int begin, end;
  71.      Integer[] arr;
  72.  
  73.      MyCallable(Integer begin, Integer end, Integer[] arr){
  74.          this.begin = begin;
  75.          this.end = end;
  76.          this.arr = arr;
  77.      }
  78.  
  79.      @Override
  80.      public Integer call() throws Exception {
  81.          try{
  82.              Integer sum = 0;
  83.             System.out.println("Thread started:" + Thread.currentThread().getId());
  84.             for(int i = begin; i < end; i++){
  85.                 sum += arr[i];
  86.             }
  87.              System.out.println("Thread finished:" + Thread.currentThread().getId());
  88.              return sum;
  89.          }catch (Exception ex){
  90.              ex.printStackTrace(System.out);
  91.          }
  92.  
  93.          return 0;
  94.      }
  95.  }
  96.  
  97.  class MyRecursiveTask extends RecursiveTask<Integer>
  98.  {
  99.      Integer[] arr;
  100.  
  101.      Integer begin, end;
  102.  
  103.      MyRecursiveTask(Integer begin, Integer end, Integer[] arr){
  104.          this.begin = begin;
  105.          this.end = end;
  106.          this.arr = arr;
  107.      }
  108.  
  109.      @Override
  110.      protected Integer compute() {
  111.  
  112.          System.out.println("Thread started:" + Thread.currentThread().getId());
  113.  
  114.          Integer sum = 0;
  115.          Integer len = end - begin;
  116.          if(len <= 400){
  117.              try {
  118.                  Thread.sleep(1000);
  119.              } catch (InterruptedException e) {
  120.              }
  121.  
  122.                  for(int i = begin; i < end; i++){
  123.                     sum += arr[i];
  124.                  }
  125.          }else{
  126.              MyRecursiveTask task1 = new MyRecursiveTask(begin, begin + len / 2, arr);
  127.              MyRecursiveTask task2 = new MyRecursiveTask( begin + len / 2 + 1, end, arr);
  128.  
  129.              task1.fork();
  130.              task2.fork();
  131.  
  132.              sum = task1.join() + task2.join();
  133.          }
  134.  
  135.          System.out.println("Thread finished:" + Thread.currentThread().getId());
  136.  
  137.          return sum;
  138.      }
  139.  }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement