(1)含返回值
问题:1+2+...+100
1.思路
分解为1+2,3+4,,...99+100,然后对这50个结果进行求和
2.任务实现
import java.util.concurrent.ExecutionException;
import java.util.concurrent.RecursiveTask;
public class CalculateTask extends RecursiveTask<Integer>{//因为需要返回值,所以使用RecursiveTask
private static final long serialVersionUID = -8486729149866588744L;
private static final int THRESHOLD = 2;// 阈值
private int start;
private int end;
public CalculateTask(int start, int end) {
this.start = start;
this.end = end;
}
@Override
protected Integer compute() {
int sum = 0;
if (end-start<THRESHOLD) {//分解为2个数的和然后计算
for(;start<=end;start++){
sum += start;
}
}else {
int mid = (start + end) >>> 1;
RecursiveTask<Integer> task1 = new CalculateTask(start, mid);
RecursiveTask<Integer> task2 = new CalculateTask(mid+1, end);
invokeAll(task1,task2);
try {
sum = task1.get()+task2.get();
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
}
}
return sum;
}
}
3.测试
package com.test;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ForkJoinPool;
import java.util.concurrent.Future;
import org.junit.Test;
public class QuickSortTest {
@Test
public void test(){
ForkJoinPool forkJoinPool = new ForkJoinPool();
// 生成一个计算任务,负责计算1+2+3+4
CalculateTask task = new CalculateTask(1, 4);
// 执行一个任务
Future<Integer> result = forkJoinPool.submit(task);
forkJoinPool.shutdown();
try {
System.out.println(result.get());
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
}
}
}
(2)无返回值
问题:快排
1.思路
根据中间值分解为2个区间,然后区间内再执行
2.任务实现
import java.util.concurrent.RecursiveAction;
public class CalculateTask extends RecursiveAction{//这里不需要返回值,所以使用RecursiveAction
private static final long serialVersionUID = 2476846494375574971L;
private int[] number = {};
private int begin;
private int end;
private static final int THRESHOLD = 1;// 阈值
public QuickSortTask(int[] number,int begin, int end){
this.number = number;
this.begin = begin;
this.end = end;
}
@Override
protected void compute() {
if (end-begin<THRESHOLD) {//只有一个数,那么不用排序了
return;
}else{
int mid = adjust(number,begin,end);
QuickSortTask task1 = new QuickSortTask(number,begin, mid - 1);
QuickSortTask task2 = new QuickSortTask(number,mid + 1, end);
invokeAll(task1, task2);
}
}
private int adjust(int[] a, int begin, int end) {
int flag = a[begin];
while (begin<end) {
while (a[end]>=flag && begin<end)
end--;
if (begin<end)
a[begin] = a[end];
while (a[begin]<=flag && begin<end)
begin++;
if (begin<end)
a[end] = a[begin];
}
a[begin] = flag;
return begin;
}
}
3.测试
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ForkJoinPool;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import org.junit.Test;
public class QuickSortTest {
@Test
public void test() {
ForkJoinPool forkJoinPool = new ForkJoinPool();
int[] a = {54,12,36,16,96,62};
QuickSortTask task = new QuickSortTask(a,0, 5);
// 执行一个任务
forkJoinPool.submit(task);
while(!task.isDone()){
}
for (int i : a) {
System.out.print(i+" ");
}
forkJoinPool.shutdown();
}
}