??????????????????????ó?????????????????????????????????????????????????????????????????????????衣?????????????? SQL ???????????????????????????????????????????????????????????????????С?????? I/O ????????????????????????? CPU ??????????????????£?????????????????????????????????????????????????? I/O ??????????????????????????????????????????????????????и?????????????????????????????? SUM??AVERAGE????????????????????????????????????б???????????????
????????????????????????????? SQL ????漰??????? I/O???????????? CUP ?????????? I/O ?????????????????????棬???????????? I/O ????????????? I/O ?????????????????????л?????????????? CPU ???????????????????????????????????????????????????????????????????????????????????????????????????????С???????????????????????????????????????????????????????????????????????????????????????????????????????????
????Divide and conquer
????????????? divide-and-conquer ?????????????????????н?????????????????????????????????????????????? divide-and-conquer ???????????????У?????????л????и?????Ч???????????д?????????
???????????? divide-and-conquer ??????????嵥 1 ?????
?????嵥 1. ??? divide-and-conquer ????????α????
????// PSEUDOCODE
????Result solve(Problem problem) {
????if (problem.size < SEQUENTIAL_THRESHOLD)
????return solveSequentially(problem);
????else {
????Result left?? right;
????INVOKE-IN-PARALLEL {
????left = solve(extractLeftHalf(problem));
????right = solve(extractRightHalf(problem));
????}
????return combine(left?? right);
????}
????}
???????? divide-and-conquer ??????????????????????????С??????????????????????????????????????С???????????б????ɡ???????????????з????????????????????????????????з????????????????????????????????????????????Щ?????????????????????з??????????????Э??????????????????Э?????? 0?????????????????????????????????????????????????Э???????????????????????????
????Fork-join
?????嵥 1 ?е?????????????????????? INVOKE-IN-PARALLEL ??????????????????????????????????????????????????????????????????????????ɡ????????????????????????к??????????з???????????? fork-join?????????????????????fork?????????????????????join??????????
?????嵥 2 ?????????????? fork-join ????????????????????????????????????е?????????????????????????? fork-join ????????????????????????????????????????
?????嵥 2. ??????????????????
????public class SelectMaxProblem {
????private final int[] numbers;
????private final int start;
????private final int end;
????public final int size;
????// constructors elided
????public int solveSequentially() {
????int max = Integer.MIN_VALUE;
????for (int i=start; i<end; i++) {
????int n = numbers[i];
????if (n > max)
????max = n;
????}
????return max;
????}
????public SelectMaxProblem subproblem(int subStart?? int subEnd) {
????return new SelectMaxProblem(numbers?? start + subStart??
????start + subEnd);
????}
????}
??????? subproblem() ??????и?????Щ?????????????????ú??????????????е???????С????? fork-join ????????к??????????????????????????????????? Problem ???? ??????????У??????????????????????????????????????????????????????????и????????????????????????????
?????嵥 3 ???????? fork-join ???? SelectMaxProblem ?????????Java 7 ???????????e???JSR 166 Expert Group ?????????????????????????????? jsr166y????????????????????? Java 6 ?????汾??????????????????? java.util.concurrent.forkjoin ?У???invoke-in-parallel ???????? coInvoke() ????????????ò???????????????????????ж?????ɡ?ForkJoinExecutor ?? Executor ???????????????????????????????????????????????????????????????????????????????????????? ForkJoinExecutor ??????????????
????fork-join ???????????? ForkJoinTasks????????Щ??????????????????????е?????????? RecursiveAction ???????? non-result-bearing ???????е??????RecursiveTask ???? result-bearing ???????????????? fork-join ????????? CyclicAction??AsyncAction ?? LinkedAsyncAction?????ù?????????????????????????? Javadoc????