博客
关于我
26. ForkJoin:单机版的MapReduce - 并发工具类
阅读量:386 次
发布时间:2019-03-05

本文共 2478 字,大约阅读时间需要 8 分钟。

???????ForkJoin??

??????

????????????????????????????????????????????????????????????????????????????

  • ??????????????????????????????????????
  • ??????????????????????????????
  • Fork/Join???

    Fork/Join???Java???????????????????????????????????????

    • Fork?????????fork()????????????????????
    • Join?????????join()?????????????????

    ForkJoin?????????????

  • ForkJoinPool????????????????ForkJoin???????????????????????????
  • ForkJoinTask???????????????????????????
    • fork()??????????
    • join()??????????????
  • ForkJoinTask????????

  • RecursiveAction?????????????????????????
  • RecursiveTask????????????????????????
  • ForkJoinPool????

    ForkJoinPool??????????-???????????????????????ForkJoinPool????????????????????????????????????????????????????????????????

    ForkJoinPool??????????????????????????????????

    ??MapReduce??????

    MapReduce???????????????????????????MapReduce??????????

    public class MyTest2 {    public static void main(String[] args) {        String[] fc = {            "hello world", "hello me", "hello fork", "hello join",            "fork join in world"        };        ForkJoinPool fjp = new ForkJoinPool(3);        MR mr = new MR(fc, 0, fc.length);        Map
    result = fjp.invoke(mr); result.forEach((k, v) -> System.out.println(k + ":" + v)); } static class MR extends RecursiveTask
    > { private String[] fc; private int start, end; MR(String[] fc, int fr, int to) { this.fc = fc; this.start = fr; this.end = to; } @Override protected Map
    compute() { if (end - start == 1) { return calc(fc[start]); } else { int mid = (start + end) / 2; MR mr1 = new MR(fc, start, mid); mr1.fork(); MR mr2 = new MR(fc, mid, end); return merge(mr2.compute(), mr1.join()); } } private Map
    merge(Map
    r1, Map
    r2) { Map
    result = new HashMap<>(); result.putAll(r1); r2.forEach((k, v) -> { Long c = result.get(k); if (c != null) { result.put(k, c + v); } else { result.put(k, v); } }); return result; } private Map
    calc(String line) { Map
    result = new HashMap<>(); String[] words = line.split("\\s+"); for (String w : words) { Long v = result.get(w); if (v != null) { result.put(w, v + 1L); } else { result.put(w, 1L); } } return result; } }}

    ??????MR???????????????????????ForkJoin?????????????????????????????????

    转载地址:http://hihwz.baihongyu.com/

    你可能感兴趣的文章
    Python 之匿名函数和偏函数
    查看>>
    python 之栈的实现
    查看>>
    python语音播放
    查看>>
    python语言:装饰器原理
    查看>>
    Python 交互式数据可视化详解
    查看>>
    python语言有哪些优点和缺点_Python有哪些优缺点,你了解吗?
    查看>>
    Python 从入门到精通:30天速成教程到底有多狠?你能坚持下来吗?
    查看>>
    Python 从数据库中存储和检索密码的最安全方法
    查看>>
    Python语言及其应用 - 知识点遍历
    查看>>
    Python 优化提速的 8 个小技巧
    查看>>
    Python 余弦相似度与皮尔逊相关系数 计算
    查看>>
    python 使用execjs 报编码错误解决办法,UnicodeDecodeError: ‘gbk‘ codec can‘t decode byte 0xac in position 145: il
    查看>>
    python 使用filetype校验文件
    查看>>
    Python 使用flush函数将缓冲区数据立即写磁盘
    查看>>
    python 使用in判断不准确,in不好使
    查看>>
    Python 使用pandas 进行查询和统计详解
    查看>>
    Redis 配置文件redis.conf详细解释
    查看>>
    python网络爬虫(2)——scrapy框架的基础使用
    查看>>
    python网络爬虫实例教程试读_Python网络爬虫实战教程(全套完整版) - 学途无忧网 - 做技术的王者 - Powered By EduSoho...
    查看>>
    Python 使用哈希函数用于加密
    查看>>