//  Copyright 2016 The Sawdust Open Source Project
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//

package sort;

import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import static array.Common.mergeInsort;

public class MergeSortRecursionMultiThreads2<T extends Comparable<T>> {
    // Runnable is enough, do not need Callable as each thread only work on its elements scope of arr
    private static class DivideMergeInSort<T extends Comparable<T>> implements Runnable {
        private T[] arr, tmp;
        private int l, r;

        @Override
        public void run() {
            if (l == r) {
                // stop divide
                return;
            }

            int mid = (l + r) / 2;

            try {
                executorService.submit(new DivideMergeInSort(arr, l, mid, tmp)).get();
                executorService.submit(new DivideMergeInSort(arr, mid + 1, r, tmp)).get();
            } catch (InterruptedException | ExecutionException e) {
                e.printStackTrace();
            }

            mergeInsort(arr, l, mid, r, tmp);
        }

        public DivideMergeInSort(T[] arr, int l, int r, T[] tmp) {
            this.arr = arr;
            this.l = l;
            this.r = r;
            this.tmp = tmp;
        }
    }

    private static ExecutorService executorService = Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors());

    public static <T extends Comparable<T>> void mergeSort(T[] arr) {
        // Input check
        if (arr == null || arr.length <= 1) { // note: arr may be empty array: {}
            return;
        }
        try {
            executorService.submit(new DivideMergeInSort(arr, 0, arr.length - 1, // note: The index is included
                    new Comparable[arr.length])).get();
        } catch (InterruptedException| ExecutionException e) {
            e.printStackTrace();
        }
    }
}

@@ -1015,33 +1015,8 @@
 Sort
-%3CT extends Comparable%3CT%3E%3E
  imp
@@ -1050,17 +1050,26 @@
 private 
-T
+Comparable
 %5B%5D arr, 
@@ -1680,25 +1680,34 @@
 MergeInSort(
-T
+Comparable
 %5B%5D arr, int 
@@ -1716,17 +1716,26 @@
  int r, 
-T
+Comparable
 %5B%5D tmp) 
@@ -2288,59 +2288,8 @@
 - 1,
- // note: The index is included%0A                   
  new
@@ -2358,16 +2358,17 @@
 xception
+ 
 %7C Execut

Trim code