content: package org.alx.article._53_fork_join_pool;
  
  import java.util.concurrent.ForkJoinPool;
  import java.util.concurrent.RecursiveAction;
  
  public class ForkJoinMergeSort {
  
      public static void main(String[] args) {
          int[] array = {38, 27, 43, 3, 9, 82, 10};
          ForkJoinPool pool = new ForkJoinPool();
          pool.invoke(new MergeSortTask(array, 0, array.length - 1));
  
          for (int i : array) {
              System.out.print(i + " ");
          }
      }
  
      static class MergeSortTask extends RecursiveAction {
          private int[] array;
          private int left;
          private int right;
  
          MergeSortTask(int[] array, int left, int right) {
              this.array = array;
              this.left = left;
              this.right = right;
          }
  
          @Override
          protected void compute() {
              if (left < right) {
                  int mid = (left + right) / 2;
  
                  MergeSortTask leftTask = new MergeSortTask(array, left, mid);
                  MergeSortTask rightTask = new MergeSortTask(array, mid + 1, right);
  
                  invokeAll(leftTask, rightTask);
  
                  merge(array, left, mid, right);
              }
          }
  
          private void merge(int[] array, int left, int mid, int right) {
              int n1 = mid - left + 1;
              int n2 = right - mid;
  
              int[] L = new int[n1];
              int[] R = new int[n2];
  
              System.arraycopy(array, left, L, 0, n1);
              System.arraycopy(array, mid + 1, R, 0, n2);
  
              int i = 0, j = 0, k = left;
              while (i < n1 && j < n2) {
                  if (L[i] <= R[j]) {
                      array[k] = L[i];
                      i++;
                  } else {
                      array[k] = R[j];
                      j++;
                  }
                  k++;
              }
  
              while (i < n1) {
                  array[k] = L[i];
                  i++;
                  k++;
              }
  
              while (j < n2) {
                  array[k] = R[j];
                  j++;
                  k++;
              }
          }
      }
  }