「这是我参与2022首次更文挑战的第26天，活动详情查看：2022首次更文挑战

# 前言

## 调用

``````ForkJoinPool();
``````
``````ForkJoinPool forkJoinPool = new ForkJoinPool(); 创建池

submit.get()获取你的结果
forkJoinPool.shutdown(); 关闭
``````

``````submit.join()
``````

# 单线程写法

``````

public static void main(String[] args) {

int[] a = {12,20,5,16,15,1,30,45,23,9};
long starttime = System.currentTimeMillis();
sort(a);
long endtime = System.currentTimeMillis();
for (int j : a) {
System.out.println(j);
}

System.out.println("用时："+(endtime-starttime));
}

public static void sort(int[] a) {
int low = 0;
int height = a.length - 1;
sort(a, low, height);
}

public static void sort(int[] a, int low, int high) {
int start = low;
int end = high;
int key = a[low];

while (end > start) {
//从后往前比较
while (end > start && a[end] >= key)
end--;
if (a[end] <= key) {
swap(a, start, end);
}
//从前往后比较
while (end > start && a[start] <= key)
start++;
if (a[start] >= key) {
swap(a, start, end);
}
//此时第一次循环比较结束，关键值的位置已经确定了。左边的值都比关键值小，右边的值都比关键值大，但是两边的顺序还有可能是不一样的，进行下面的递归调用
}
//递归
if (start > low)
sort(a, low, start - 1);//左边序列。第一个索引位置到关键值索引-1
if (end < high)
sort(a, end + 1, high);//右边序列。从关键值索引+1到最后一个
}

public static void swap(int[] a, int start, int end) {
int temp = a[start];
a[start] = a[end];
a[end] = temp;
}

}
``````

# 多线程写法

``````
public class ManyFastSort extends RecursiveTask<Void> {
private CopyOnWriteArrayList<Integer> a;
private int low;
private int height;
private int start;
private int end;

public ManyFastSort(CopyOnWriteArrayList<Integer> a, int low, int height) {
this.a = a;
this.low = low;
this.height = height;
}

public void swap(CopyOnWriteArrayList<Integer> a, int start, int end) {
int temp = a.get(start);
a.set(start,a.get(end));
a.set(end,temp);
}

public void sortonece(CopyOnWriteArrayList<Integer>a,int start,int end){
int key = a.get(start);
while (end > start) {
//从后往前比较
while (end > start && a.get(end) >= key)
end--;
if (a.get(end) <= key) {
swap(a, start, end);
}
//从前往后比较
while (end > start && a.get(start) <= key)
start++;
if (a.get(start) >= key) {
swap(a, start, end);
}

}
this.start = start;
this.end = end;

}

@Override
protected Void compute() {
start = low;
end = height;
ManyFastSort rightsort=null;
ManyFastSort leftsort = null;

sortonece(a,start,end);

if(start>low){

leftsort = new ManyFastSort(a, low, start - 1);
}
if(end<height){

rightsort = new ManyFastSort(a, end + 1, height);
}

if(rightsort!=null){
rightsort.fork();
rightsort.join();
}
if(leftsort!=null){
leftsort.fork();
leftsort.join();
}

return null;
}

}

class TestManyFastSort{
public static void main(String[] args) {
int[] a = {12,20,5,16,15,1,30,45,23,9};

ArrayList<Integer> integerArray = (ArrayList<Integer>) Arrays.stream(a).boxed().collect(Collectors.toList());
CopyOnWriteArrayList<Integer> integers = new CopyOnWriteArrayList<>(integerArray);
ManyFastSort manyFastSort = new ManyFastSort(integers, 0, integers.size()- 1);
ForkJoinPool forkJoinPool = new ForkJoinPool();

long starttime = System.currentTimeMillis();

submit.join();

long endtime = System.currentTimeMillis();
for (int j : integers) {
System.out.println(j);
}
System.out.println("用时："+(endtime-starttime));
forkJoinPool.shutdown();
}
}
``````

# 效率分析

``````CopyOnWriteArrayList
``````