对于我的工作,我必须开发一个小型Java应用程序,该应用程序可以解析非常大的文本文件(〜6800 kb),因此我正在尝试对其进行一些优化,以进行进一步的处理。用等号(=)分割线作为第一个fork任务,并使用逗号分割和使用第二个fork任务进行一些计算。这会导致针对单个应用程序的多个fork任务带来良好的性能。

1)第一个fork和join过程:

 @Override
protected Map < String, String > compute() {
SplitString lineSplit = new SplitString();
Map < String, String > splitUrl = new HashMap < > ();
// list size is less than limit process actual task.

if (readAllLines.size() < Constant.LIMIT) {
    // splitUrl map stores the splitter lines with equal_to
    splitUrl = lineSplit.splittingLines(readAllLines);

} else { // list size greater than limit splits task in to two and process it.
    // split task into sub task and stores it in list
    List < String > left = readAllLines.subList(Constant.ZERO, readAllLines.size() / 2);
    List < String > right = readAllLines.subList(readAllLines.size() / 2, readAllLines.size());
    SplitTask firstTask = new SplitTask(left);
    SplitTask secondTask = new SplitTask(right);
    // forks the firstTask
    firstTask.fork();
    // forks the secondTask
    secondTask.fork();
    // join the task in splitUrl map
    splitUrl.putAll(firstTask.join());
    splitUrl.putAll(secondTask.join());
}
return splitUrl;
}


2)第二个fork和join过程:

 @Override
protected Map < String, Map < String, Integer >> compute() {
    SplitString ruleSplit = new SplitString();
    Map < String, Map < String, Integer >> response = new HashMap < > ();
    // list size is less than limit process actual task.

    if (KeyList.size() < Constant.LIMIT) {
        // splitUrl map stores the splitter lines with equal_to
        response = ruleSplit.calculatingRuleTime(result, KeyList);

    } else { // list size greater than limit splits task in to two and process it.
        // split task into sub task and stores it in list.
        List < String > left = KeyList.subList(Constant.ZERO, KeyList.size() / 2);
        List < String > right = KeyList.subList(KeyList.size() / 2, KeyList.size());
        SplitRuleTask firstTask = new SplitRuleTask(result, left);
        SplitRuleTask secondTask = new SplitRuleTask(result, right);
        // forks the firstTask
        firstTask.fork();
        // forks the firstTask
        secondTask.fork();
        // join the task in response map
        response.putAll(firstTask.join());
        response.putAll(secondTask.join());
    }
    return response;
}


有人可以帮我吗?

最佳答案

叉也造成了沉重的负担。我建议您阅读线程概念,然后在它们上实现多线程。
更高级的方法是创建要使用的线程池。建议您阅读以下链接。

关于java - 使用多个fork和join类拆分任务,这是一个好习惯吗?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/42943746/

10-09 00:18