Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.company;
- import java.util.ArrayList;
- import java.util.Random;
- import java.util.concurrent.*;
- public class Main {
- static final int MAX = 100;
- static Integer[] arr = new Integer[100000000];
- static Integer maximum = 10, minimum = 1000000;
- public static void main(String[] args) throws Exception {
- LoadArr();
- ImplementationThreadPoolExecutor(4);
- ImplementationForkJoinPool();
- }
- public static void LoadArr(){
- for(int i = 0; i < MAX; i++){
- Random rn = new Random();
- int n = maximum - minimum + 1;
- int a = rn.nextInt() % n;
- int randomNum = minimum + a;
- arr[i] = randomNum;
- }
- }
- public static void ImplementationThreadPoolExecutor(Integer countThread) throws Exception {
- ThreadPoolExecutor es = new ThreadPoolExecutor(
- 20, 40, 1, TimeUnit.MILLISECONDS,
- new LinkedBlockingQueue<>(30), new MyReject()
- );
- ArrayList<Future<Integer>> subArr = new ArrayList<Future<Integer>>(100);
- for (int i = 0; i < countThread; i++){
- Integer begin = (MAX / countThread) * i;
- Integer end = (MAX / countThread) * (i + 1);
- subArr.add(es.submit(new MyCallable(begin, end, arr)));
- }
- Integer sum = 0;
- for(int i = 0; i < countThread; i++){
- sum += subArr.get(i).get();
- }
- es.shutdown();
- }
- public static void ImplementationForkJoinPool(){
- ForkJoinPool pool = new ForkJoinPool(4);
- System.out.println(pool.invoke(new MyRecursiveTask(0, MAX, arr)));
- }
- }
- class MyReject implements RejectedExecutionHandler{
- @Override
- public void rejectedExecution(Runnable r, ThreadPoolExecutor executor) {
- System.out.println("REJECTED");
- }
- }
- class MyCallable implements Callable<Integer>
- {
- int begin, end;
- Integer[] arr;
- MyCallable(Integer begin, Integer end, Integer[] arr){
- this.begin = begin;
- this.end = end;
- this.arr = arr;
- }
- @Override
- public Integer call() throws Exception {
- try{
- Integer sum = 0;
- System.out.println("Thread started:" + Thread.currentThread().getId());
- for(int i = begin; i < end; i++){
- sum += arr[i];
- }
- System.out.println("Thread finished:" + Thread.currentThread().getId());
- return sum;
- }catch (Exception ex){
- ex.printStackTrace(System.out);
- }
- return 0;
- }
- }
- class MyRecursiveTask extends RecursiveTask<Integer>
- {
- Integer[] arr;
- Integer begin, end;
- MyRecursiveTask(Integer begin, Integer end, Integer[] arr){
- this.begin = begin;
- this.end = end;
- this.arr = arr;
- }
- @Override
- protected Integer compute() {
- System.out.println("Thread started:" + Thread.currentThread().getId());
- Integer sum = 0;
- Integer len = end - begin;
- if(len <= 400){
- try {
- Thread.sleep(1000);
- } catch (InterruptedException e) {
- }
- for(int i = begin; i < end; i++){
- sum += arr[i];
- }
- }else{
- MyRecursiveTask task1 = new MyRecursiveTask(begin, begin + len / 2, arr);
- MyRecursiveTask task2 = new MyRecursiveTask( begin + len / 2 + 1, end, arr);
- task1.fork();
- task2.fork();
- sum = task1.join() + task2.join();
- }
- System.out.println("Thread finished:" + Thread.currentThread().getId());
- return sum;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement