JPC*_*JPC 6 java parallel-processing recursion multithreading
我有递归代码,以深度优先的方式处理树结构.代码基本上如下所示:
function(TreeNode curr)
{
if (curr.children != null && !curr.children.isEmpty())
{
for (TreeNode n : curr.children)
{
//do some stuff
function(n);
}
}
else
{
//do some other processing
}
}
Run Code Online (Sandbox Code Playgroud)
我想使用线程来加快完成速度.大部分时间都花在遍历上,所以我不想只创建一个线程来处理"其他处理",因为它不需要那么长时间.我想我想在"做一些事情"时分叉线程,但是如何工作呢?
axt*_*avt 12
这是一个包含在Java 7中的Fork/Join框架的好例子.作为用于Java 6的独立库,可以在此处下载.
像这样的东西:
public class TreeTask extends RecursiveAction {
private final TreeNode node;
private final int level;
public TreeTask(TreeNode node, int level) {
this.node = node;
this.level = leve;
}
public void compute() {
// It makes sense to switch to single-threaded execution after some threshold
if (level > THRESHOLD) function(node);
if (node.children != null && !node.children.isEmpty()) {
List<TreeTask> subtasks = new ArrayList<TreeTask>(node.children.size());
for (TreeNode n : node.children) {
// do some stuff
subtasks.add(new TreeTask(n, level + 1));
}
invokeAll(subtasks); // Invoke and wait for completion
} else {
//do some other processing
}
}
}
...
ForkJoinPool p = new ForkJoinPool(N_THREADS);
p.invoke(root, 0);
Run Code Online (Sandbox Code Playgroud)
fork/join框架的关键点是工作窃取 - 在等待子任务线程完成时执行其他任务.它允许您以直接的方式编写算法,同时避免线程耗尽的问题作为一个天真的apporach与ExecutorService将有.