看了下Java Tutorials中的fork/join章节,整理下。

什么是fork/join框架

  fork/join框架是ExecutorService接口的一个实现,可以帮助开发人员充分利用多核处理器的优势,编写出并行执行的程序,提高应用程序的性能;设计的目的是为了处理那些可以被递归拆分的任务。

  fork/join框架与其它ExecutorService的实现类相似,会给线程池中的线程分发任务,不同之处在于它使用了工作窃取算法,所谓工作窃取,指的是对那些处理完自身任务的线程,会从其它线程窃取任务执行。

  fork/join框架的核心是ForkJoinPool类,该类继承了AbstractExecutorService类。ForkJoinPool实现了工作窃取算法并且能够执行 ForkJoinTask任务。

基本使用方法

  在使用fork/join框架之前,我们需要先对任务进行分割,任务分割代码应该跟下面的伪代码类似:

  1. if (任务足够小){
  2. 直接执行该任务;
    }else{
  3. 将任务一分为二;
  4. 执行这两个任务并等待结果;
    }

  首先,我们会在ForkJoinTask的子类中封装以上代码,不过一般我们会使用更加具体的ForkJoinTask类型,如 RecursiveTask(可以返回一个结果)或RecursiveAction

  当写好ForkJoinTask的子类后,创建该对象,该对象代表了所有需要完成的任务;然后将这个任务对象传给ForkJoinPool实例的invoke()去执行即可。

例子-图像模糊

  为了更加直观的理解fork/join框架是如何工作的,可以看一下下面这个例子。假定我们有一个图像模糊的任务需要完成,原始图像数据可以用一个整型数组表示,每一个整型元素包含了一个像素点的颜色值(RBG,存放在整型元素的不同位中)。目标图像同样是由一个整型数组构成,每个整型元素包含RBG颜色信息;

  执行模糊操作需要遍历原始图像整型数组的每个元素,并对其周围的像素点做均值操作(RGB均值),然后将结果存放到目标数组中。由于图像是一个大数组,这个处理操作会花费一定的时间。但是有了fork/join框架,我们可以充分利用多核处理器进行并行计算。如下是一个可能的代码实现(图像做水平方向的模糊操作):

Tips:该例子仅仅是阐述fork/join框架的使用,并不推荐使用该方法做图像模糊,图像边缘处理也没做判断

  1. public class ForkBlur extends RecursiveAction {
  2. private static final long serialVersionUID = -8032915917030559798L;
  3. private int[] mSource;
  4. private int mStart;
  5. private int mLength;
  6. private int[] mDestination;
  7. private int mBlurWidth = 15; // Processing window size, should be odd.
  8.  
  9. public ForkBlur(int[] src, int start, int length, int[] dst) {
  10. mSource = src;
  11. mStart = start;
  12. mLength = length;
  13. mDestination = dst;
  14. }
  15.  
  16. // Average pixels from source, write results into destination.
  17. protected void computeDirectly() {
  18. int sidePixels = (mBlurWidth - 1) / 2;
  19. for (int index = mStart; index < mStart + mLength; index++) {
  20. // Calculate average.
  21. float rt = 0, gt = 0, bt = 0;
  22. for (int mi = -sidePixels; mi <= sidePixels; mi++) {
  23. int mindex = Math.min(Math.max(mi + index, 0), mSource.length - 1);
  24. int pixel = mSource[mindex];
  25. rt += (float) ((pixel & 0x00ff0000) >> 16) / mBlurWidth;
  26. gt += (float) ((pixel & 0x0000ff00) >> 8) / mBlurWidth;
  27. bt += (float) ((pixel & 0x000000ff) >> 0) / mBlurWidth;
  28. }
  29.  
  30. // Re-assemble destination pixel.
  31. int dpixel = (0xff000000)
  32. | (((int) rt) << 16)
  33. | (((int) gt) << 8)
  34. | (((int) bt) << 0);
  35. mDestination[index] = dpixel;
  36. }
  37. }
  38. ...

  现在,我们开始编写compute()的实现方法,该方法分成两部分:直接执行模糊操作和任务的划分;一个数组长度阈值sThreshold可以帮助我们决定任务是直接执行还是进行划分;

  1. @Override
  2. protected void compute() {
  3. if (mLength < sThreshold) {
  4. computeDirectly();
  5. return;
  6. }
  7.  
  8. int split = mLength / 2;
  9.  
  10. invokeAll(new ForkBlur(mSource, mStart, split, mDestination),
  11. new ForkBlur(mSource, mStart + split, mLength - split,
  12. mDestination));
  13. }

接下来按如下步骤即可完成图像模糊任务啦:

1、创建图像模糊任务

  1. ForkBlur fb = new ForkBlur(src, 0, src.length, dst);

2、创建ForkJoinPool

  1. ForkJoinPool pool = new ForkJoinPool();

3、执行图像模糊任务

  1. pool.invoke(fb);

完整代码如下:

  1. /*
  2. * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved.
  3. *
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions
  6. * are met:
  7. *
  8. * - Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. *
  11. * - Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in the
  13. * documentation and/or other materials provided with the distribution.
  14. *
  15. * - Neither the name of Oracle or the names of its
  16. * contributors may be used to endorse or promote products derived
  17. * from this software without specific prior written permission.
  18. *
  19. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
  20. * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  21. * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  22. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  23. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  24. * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  25. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  26. * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  27. * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  28. * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  29. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  30. */
  31.  
  32. import java.awt.image.BufferedImage;
  33. import java.io.File;
  34. import java.util.concurrent.ForkJoinPool;
  35. import java.util.concurrent.RecursiveAction;
  36. import javax.imageio.ImageIO;
  37.  
  38. /**
  39. * ForkBlur implements a simple horizontal image blur. It averages pixels in the
  40. * source array and writes them to a destination array. The sThreshold value
  41. * determines whether the blurring will be performed directly or split into two
  42. * tasks.
  43. *
  44. * This is not the recommended way to blur images; it is only intended to
  45. * illustrate the use of the Fork/Join framework.
  46. */
  47. public class ForkBlur extends RecursiveAction {
  48. private static final long serialVersionUID = -8032915917030559798L;
  49. private int[] mSource;
  50. private int mStart;
  51. private int mLength;
  52. private int[] mDestination;
  53. private int mBlurWidth = 15; // Processing window size, should be odd.
  54.  
  55. public ForkBlur(int[] src, int start, int length, int[] dst) {
  56. mSource = src;
  57. mStart = start;
  58. mLength = length;
  59. mDestination = dst;
  60. }
  61.  
  62. // Average pixels from source, write results into destination.
  63. protected void computeDirectly() {
  64. int sidePixels = (mBlurWidth - 1) / 2;
  65. for (int index = mStart; index < mStart + mLength; index++) {
  66. // Calculate average.
  67. float rt = 0, gt = 0, bt = 0;
  68. for (int mi = -sidePixels; mi <= sidePixels; mi++) {
  69. int mindex = Math.min(Math.max(mi + index, 0), mSource.length - 1);
  70. int pixel = mSource[mindex];
  71. rt += (float) ((pixel & 0x00ff0000) >> 16) / mBlurWidth;
  72. gt += (float) ((pixel & 0x0000ff00) >> 8) / mBlurWidth;
  73. bt += (float) ((pixel & 0x000000ff) >> 0) / mBlurWidth;
  74. }
  75.  
  76. // Re-assemble destination pixel.
  77. int dpixel = (0xff000000)
  78. | (((int) rt) << 16)
  79. | (((int) gt) << 8)
  80. | (((int) bt) << 0);
  81. mDestination[index] = dpixel;
  82. }
  83. }
  84. protected static int sThreshold = 10000;
  85.  
  86. @Override
  87. protected void compute() {
  88. if (mLength < sThreshold) {
  89. computeDirectly();
  90. return;
  91. }
  92.  
  93. int split = mLength / 2;
  94.  
  95. invokeAll(new ForkBlur(mSource, mStart, split, mDestination),
  96. new ForkBlur(mSource, mStart + split, mLength - split,
  97. mDestination));
  98. }
  99.  
  100. // Plumbing follows.
  101. public static void main(String[] args) throws Exception {
  102. String srcName = "C:\\test6.jpg";
  103. File srcFile = new File(srcName);
  104. BufferedImage image = ImageIO.read(srcFile);
  105.  
  106. System.out.println("Source image: " + srcName);
  107.  
  108. BufferedImage blurredImage = blur(image);
  109.  
  110. String dstName = "C:\\test6_out.jpg";
  111. File dstFile = new File(dstName);
  112. ImageIO.write(blurredImage, "jpg", dstFile);
  113.  
  114. System.out.println("Output image: " + dstName);
  115.  
  116. }
  117.  
  118. public static BufferedImage blur(BufferedImage srcImage) {
  119. int w = srcImage.getWidth();
  120. int h = srcImage.getHeight();
  121.  
  122. int[] src = srcImage.getRGB(0, 0, w, h, null, 0, w);
  123. int[] dst = new int[src.length];
  124.  
  125. System.out.println("Array size is " + src.length);
  126. System.out.println("Threshold is " + sThreshold);
  127.  
  128. int processors = Runtime.getRuntime().availableProcessors();
  129. System.out.println(Integer.toString(processors) + " processor"
  130. + (processors != 1 ? "s are " : " is ")
  131. + "available");
  132.  
  133. ForkBlur fb = new ForkBlur(src, 0, src.length, dst);
  134.  
  135. ForkJoinPool pool = new ForkJoinPool();
  136.  
  137. long startTime = System.currentTimeMillis();
  138. pool.invoke(fb);
  139. long endTime = System.currentTimeMillis();
  140.  
  141. System.out.println("Image blur took " + (endTime - startTime) +
  142. " milliseconds.");
  143.  
  144. BufferedImage dstImage =
  145. new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB);
  146. dstImage.setRGB(0, 0, w, h, dst, 0, w);
  147.  
  148. return dstImage;
  149. }
  150. }

测试了一下,执行效果如下:

Source image: C:\test6.jpg
Array size is 120000
Threshold is 10000
4 processors are available
Image blur took 10 milliseconds.
Output image: C:\test6_out.jpg

JDK中使用fork/join的例子

  除了我们上面提到的使用fork/join框架并行执行图像模糊任务之外,在JAVA SE中,也已经利用fork/join框架实现了一些非常有用的特性。其中一个实现是在JAVA SE8 中java.util.Arrays 类的parallelSort()方法。这些方法和sort()方法类似,但是可以通过fork/join框架并行执行。对于大数组排序,在多核处理器系统中,使用并行排序方法比顺序排序更加高效。当然,关于这些排序方法是如何利用fork/join框架不在本篇文章讨论范围,更多信息可以查看JAVA API文档。
  另一个fork/join框架的实现是在JAVA SE8中的java.util.streams包内,与Lambda表达式相关,更多信息,可以查看https://docs.oracle.com/javase/tutorial/java/javaOO/lambdaexpressions.html链接。

参考链接:https://docs.oracle.com/javase/tutorial/essential/concurrency/forkjoin.html

JAVA中的Fork/Join框架的更多相关文章

  1. java 中的fork join框架

    文章目录 ForkJoinPool ForkJoinWorkerThread ForkJoinTask 在ForkJoinPool中提交Task java 中的fork join框架 fork joi ...

  2. Java 并发编程 -- Fork/Join 框架

    概述 Fork/Join 框架是 Java7 提供的一个用于并行执行任务的框架,是一个把大任务分割成若干个小任务,最终汇总每个小任务结果后得到大任务结果的框架.下图是网上流传的 Fork Join 的 ...

  3. Java 并发之 Fork/Join 框架

    什么是 Fork/Join 框架 Fork/Join 框架是一种在 JDk 7 引入的线程池,用于并行执行把一个大任务拆成多个小任务并行执行,最终汇总每个小任务结果得到大任务结果的特殊任务.通过其命名 ...

  4. Java并发编程--Fork/Join框架使用

    上篇博客我们介绍了通过CyclicBarrier使线程同步,可是上述方法存在一个问题,那就是假设一个大任务跑了2个线程去完毕.假设线程2耗时比线程1多2倍.线程1完毕后必须等待线程2完毕.等待的过程线 ...

  5. Java 7 Fork/Join 框架

    在 Java7引入的诸多新特性中,Fork/Join 框架无疑是重要的一项.JSR166旨在标准化一个实质上可扩展的框架,以将并行计算的通用工具类组织成一个类似java.util中Collection ...

  6. ☕【Java技术指南】「并发编程专题」Fork/Join框架基本使用和原理探究(基础篇)

    前提概述 Java 7开始引入了一种新的Fork/Join线程池,它可以执行一种特殊的任务:把一个大任务拆成多个小任务并行执行. 我们举个例子:如果要计算一个超大数组的和,最简单的做法是用一个循环在一 ...

  7. java多线程8:阻塞队列与Fork/Join框架

    队列(Queue),是一种数据结构.除了优先级队列和LIFO队列外,队列都是以FIFO(先进先出)的方式对各个元素进行排序的. BlockingQueue 而阻塞队列BlockingQueue除了继承 ...

  8. 013-多线程-基础-Fork/Join框架、parallelStream讲解

    一.概述 Fork/Join框架是Java7提供了的一个用于并行执行任务的框架, 是一个把大任务分割成若干个小任务,最终汇总每个小任务结果后得到大任务结果的框架. 它同ThreadPoolExecut ...

  9. Java并发——Fork/Join框架

    为了防止无良网站的爬虫抓取文章,特此标识,转载请注明文章出处.LaplaceDemon/ShiJiaqi. http://www.cnblogs.com/shijiaqi1066/p/4631466. ...

随机推荐

  1. WCF 实体更改发布后,如何不影响调用方?

    应用场景:使用 WCF 有一个坏处,就是如果我们经常对 WCF 应用程序更新,有时候调用方也要进行 Update Service,但调用方往往会很多,那么这个工作就会很讨厌,比如 WCF Servic ...

  2. ZOJ Problem Set - 1048 Financial Management

    我承认这是一道水的不能再水的题,今天一下就做到了,还是无耻的帖上来吧 #include <stdio.h> int main() { double sum=0; for(int i=1;i ...

  3. [linux]挂载smb

    什么是 smb 服务器消息区块(英语:Server Message Block,缩写为SMB,服务器消息区块),又称网络文件共享系统(英语:Common Internet File System,缩写 ...

  4. 由项目浅谈JS中MVVM模式

    文章版权由作者李晓晖和博客园共有,若转载请于明显处标明出处:http://www.cnblogs.com/naaoveGIS/. 1.    背景 最近项目原因使用了durandal.js和knock ...

  5. Microsoft Azure News(6) Azure新F系列虚拟机

    <Windows Azure Platform 系列文章目录> 我们知道Azure虚拟机的CPU和内存是固定搭配的,不可以按照用户的想法随意更改. 在有些时候,我们需要虚拟机CPU核心数量 ...

  6. SQL高性能查询优化语句(总结)

    SQL 高性能查询优化语句,一些经验总结 1.应尽量避免在 where 子句中对字段进行 null 值判断,否则将导致引擎放弃使用索引而进行全表扫描,如:select id from t where ...

  7. C#动态编译

    C#提供了DynamicObject和IDynamicMetaObjectProvider两种方式实现动态类型,动态类型的好处是类型的公有接口可以在运行时改变. 创建动态类型最简单的方法就是继承Dyn ...

  8. UIAlertController 部分用法及属性

    //创建UIAlertController:初始化UIAlertController 需要使用alertControllerWithTitle UIAlertController *alertCont ...

  9. Struts2入门(一)——环境搭建和简单例子(Struts2 2.5.2版本)

    一.前言 1.了解三大框架 什么是框架? 框架是一种规范,一种规则,一种把技术组织起来的规则,这就是框架. 什么是三大框架(SSH),Struts.hibernate和spring的作用是什么? St ...

  10. Bootstrap分为几部分?

    Bootstrap分为五部分: (1)起步(Startup) (2)全局CSS样式(Global CSS) (3)组件(Component) (4)插件(Plugin) (5)定制(Customize ...