栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

将ExecutorService与任务树结合使用以执行

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

将ExecutorService与任务树结合使用以执行

Java
7的概念

ForkJoinPool
允许任务通过将任务提交给同一执行者来“分叉”另一个任务。然后为它提供一个选项,供以后尝试通过尝试运行“帮助加入”该任务(如果尚未运行)的选项提供。

我相信,通过简单地将

Executor
with与组合,可以在Java 6中完成相同的操作
FutureTask
。像这样:

public class Fib implements Callable<Integer> {    int n;    Executor exec;    Fib(final int n, final Executor exec) {        this.n = n;        this.exec = exec;    }        @Override    public Integer call() throws Exception {        if (n == 0 || n == 1) { return n;        }        //Divide the problem        final Fib n1 = new Fib(n - 1, exec);        final Fib n2 = new Fib(n - 2, exec);        //FutureTask only allows run to complete once        final FutureTask<Integer> n2Task = new FutureTask<Integer>(n2);        //Ask the Executor for help        exec.execute(n2Task);        //Do half the work ourselves        final int partialResult = n1.call();        //Do the other half of the work if the Executor hasn't        n2Task.run();        //Return the combined result        return partialResult + n2Task.get();    }}


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/428543.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号