Created
November 27, 2016 13:58
-
-
Save junwen12221/8d87396d3919d10dee5ac2a155de3c63 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package seven; | |
import java.nio.file.Path; | |
import java.util.ArrayList; | |
import java.util.List; | |
import java.util.concurrent.ExecutionException; | |
import java.util.concurrent.RecursiveTask; | |
import java.util.stream.Collector; | |
import java.util.stream.Collectors; | |
import java.util.stream.IntStream; | |
/** | |
* Created by Administrator on 2016/11/26 0026. | |
*/ | |
public class FileTask extends RecursiveTask<Long> { | |
Path path; | |
List<String> lines; | |
String word; | |
int start; | |
int end; | |
static final int THRESHOLD = 2; | |
public FileTask(Path path, List<String> lines, int start, int end, String word) { | |
this.path = path; | |
this.lines = lines; | |
this.word = word; | |
this.start = start; | |
this.end = end; | |
} | |
@Override | |
protected Long compute() { | |
long sum = 0;// 结果 模板代码 | |
boolean canCompute = (end - start) < THRESHOLD;//模板代码 | |
if (canCompute) {//模板代码 | |
//开始计算 | |
sum = lines.subList(start, end).stream().filter(i -> i.contains(word)).count(); | |
} else {//模板代码 | |
int taskNumber = 1; | |
int step = (start + end) / taskNumber; | |
ArrayList<FileTask> subTasks = new ArrayList<>(); | |
int pos = start; | |
for (int i = 0; i < taskNumber; ++i) { | |
int lastOne = pos + step; | |
if (lastOne > end) { | |
lastOne = end; | |
} | |
if((lastOne-pos)<0){ | |
System.out.println("====> "); | |
} | |
FileTask subTask = new FileTask(path,lines, pos, lastOne, word); | |
pos += step + 1; | |
subTasks.add(subTask); | |
subTask.fork(); | |
} | |
for (FileTask task : subTasks) { | |
//开始计算 reduce | |
sum+=task.join(); | |
} | |
} | |
return sum; | |
} | |
long count(List<String> line, int start, int end, String word) { | |
return IntStream.range(start, end).filter(i -> line.get(i).equals(word)).count(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment