import java.util.concurrent.RecursiveAction;

public class ForkJoinTask extends RecursiveAction {
    int[][] matrix;
    int[][] sorted;
    Integer[] indices;
    int start, end;
    static final int THRESHOLD = 5;

    ForkJoinTask(int[][] matrix, int[][] sorted, Integer[] indices, int start, int end) {
        this.matrix = matrix;
        this.sorted = sorted;
        this.indices = indices;
        this.start = start;
        this.end = end;
    }

    @Override
    protected void compute() {
        if (end - start <= THRESHOLD) {
            for (int i = start; i < end; i++) {
                for (int row = 0; row < matrix.length; row++) {
                    sorted[row][i] = matrix[row][indices[i]];
                }
            }
        } else {
            int mid = (start + end) / 2;
            invokeAll(new ForkJoinTask(matrix, sorted, indices, start, mid), new ForkJoinTask(matrix, sorted, indices
                    , mid, end));
        }
    }
}
