使用Java7提供的Fork/Join框架
http://blog.csdn.net/a352193394/article/details/39872923
版权声明:本文为博主原创文章,未经博主允许不得转载。
在Java7中,JDK提供对多线程开发提供了一个非常强大的框架,就是Fork/Join框架。这个是对原来的Executors更
进一步,在原来的基础上增加了并行分治计算中的一种Work-stealing策略,就是指的是。当一个线程正在等待他创建的
子线程运行的时候,当前线程如果完成了自己的任务后,就会寻找还没有被运行的任务并且运行他们,这样就是和
Executors这个方式最大的区别,更加有效的使用了线程的资源和功能。所以非常推荐使用Fork/Join框架。
下面我们以一个例子来说明这个框架如何使用,主要就是创建一个含有10000个资源的List,分别去修改他的内容。
- package com.bird.concursey.charpet8;
- /**
- * store the name and price of a product
- * @author bird 2014年10月7日 下午11:23:14
- */
- public class Product {
- private String name;
- private double price;
- public String getName() {
- return name;
- }
- public void setName(String name) {
- this.name = name;
- }
- public double getPrice() {
- return price;
- }
- public void setPrice(double price) {
- this.price = price;
- }
- }
- package com.bird.concursey.charpet8;
- import java.util.ArrayList;
- import java.util.List;
- /**
- * generate a list of random products
- * @author bird
- * 2014年10月7日 下午11:24:47
- */
- public class ProductListGenerator {
- public List<Product> generate(int size) {
- List<Product> list = new ArrayList<Product>();
- for(int i = 0 ; i < size; i++) {
- Product product = new Product();
- product.setName("Product" + i);
- product.setPrice(10);
- list.add(product);
- }
- return list;
- }
- }
- package com.bird.concursey.charpet8;
- import java.util.List;
- import java.util.concurrent.ForkJoinPool;
- import java.util.concurrent.RecursiveAction;
- import java.util.concurrent.TimeUnit;
- public class Task extends RecursiveAction {
- private static final long serialVersionUID = 1L;
- // These attributes will determine the block of products this task has to
- // process.
- private List<Product> products;
- private int first;
- private int last;
- // store the increment of the price of the products
- private double increment;
- public Task(List<Product> products, int first, int last, double increment) {
- super();
- this.products = products;
- this.first = first;
- this.last = last;
- this.increment = increment;
- }
- /**
- * If the difference between the last and first attributes is greater than
- * or equal to 10, create two new Task objects, one to process the first
- * half of products and the other to process the second half and execute
- * them in ForkJoinPool using the invokeAll() method.
- */
- @Override
- protected void compute() {
- if (last - first < 10) {
- updatePrices();
- } else {
- int middle = (first + last) / 2;
- System.out.printf("Task: Pending tasks:%s\n", getQueuedTaskCount());
- Task t1 = new Task(products, first, middle + 1, increment);
- Task t2 = new Task(products, middle + 1, last, increment);
- invokeAll(t1, t2);
- }
- }
- private void updatePrices() {
- for (int i = first; i < last; i++) {
- Product product = products.get(i);
- product.setPrice(product.getPrice() * (1 + increment));
- }
- }
- public static void main(String[] args) {
- ProductListGenerator productListGenerator = new ProductListGenerator();
- List<Product> products = productListGenerator.generate(10000);
- Task task = new Task(products, 0, products.size(), 0.2);
- ForkJoinPool pool = new ForkJoinPool();
- pool.execute(task);
- do {
- System.out.printf("Main: Thread Count: %d\n",
- pool.getActiveThreadCount());
- System.out.printf("Main: Thread Steal: %d\n", pool.getStealCount());
- System.out.printf("Main: Parallelism: %d\n", pool.getParallelism());
- try {
- TimeUnit.MILLISECONDS.sleep(5);
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- } while (!task.isDone());
- pool.shutdown();
- if(task.isCompletedNormally()) {
- System.out.printf("Main: The process has completed normally.\n");
- }
- for(Product product : products) {
- if(product.getPrice() != 12) {
- System.out.printf("Product %s: %f\n",product.getName(),product.getPrice());
- }
- }
- System.out.println("Main: End of the program.\n");
- }
- }
In this example, you have created a ForkJoinPool object and a subclass of the
ForkJoinTask class that you execute in the pool. To create the ForkJoinPool object,
you have used the constructor without arguments, so it will be executed with its default
configuration. It creates a pool with a number of threads equal to the number of processors
of the computer. When the ForkJoinPool object is created, those threads are created and
they wait in the pool until some tasks arrive for their execution.
Since the Task class doesn't return a result, it extends the RecursiveAction class. In the
recipe, you have used the recommended structure for the implementation of the task. If the
task has to update more than 10 products, it divides those set of elements into two blocks,
creates two tasks, and assigns a block to each task. You have used the first and last
attributes in the Task class to know the range of positions that this task has to update in the
list of products. You have used the first and last attributes to use only one copy of the
products list and not create different lists for each task.
To execute the subtasks that a task creates, it calls the invokeAll() method. This is a
synchronous call, and the task waits for the finalization of the subtasks before continuing
(potentially finishing) its execution. While the task is waiting for its subtasks, the worker thread
that was executing it takes another task that was waiting for execution and executes it. With
this behavior, the Fork/Join framework offers a more efficient task management than the
Runnable and Callable objects themselves.
between the Executor and the Fork/Join framework. In the Executor framework, all the tasks
have to be sent to the executor, while in this case, the tasks include methods to execute and
control the tasks inside the pool. You have used the invokeAll() method in the Task class,
that extends the RecursiveAction class that extends the ForkJoinTask class.
method. In this case, it's an asynchronous call, and the main thread continues its execution.
You have used some methods of the ForkJoinPool class to check the status and the
evolution of the tasks that are running. The class includes more methods that can be useful
for this purpose. See the Monitoring a Fork/Join pool recipe for a complete list of
those methods.
shutdown() method.
使用Java7提供的Fork/Join框架的更多相关文章
- Java 7 Fork/Join 框架
在 Java7引入的诸多新特性中,Fork/Join 框架无疑是重要的一项.JSR166旨在标准化一个实质上可扩展的框架,以将并行计算的通用工具类组织成一个类似java.util中Collection ...
- Java开发笔记(一百零六)Fork+Join框架实现分而治之
前面依次介绍了普通线程池和定时器线程池的用法,这两种线程池有个共同点,就是线程池的内部线程之间并无什么关联,然而某些情况下的各线程间存在着前因后果关系.譬如人口普查工作,大家都知道我国总人口为14亿左 ...
- 使用Java7提供Fork/Join框架
在Java7在.JDK它提供了多线程开发提供了一个非常强大的框架.这是Fork/Join框架.这是原来的Executors更多 进一步,在原来的基础上添加了并行分治计算中的一种Work-stealin ...
- Java7任务并行执行神器:Fork&Join框架
Fork/Join是什么? Fork/Join框架是Java7提供的并行执行任务框架,思想是将大任务分解成小任务,然后小任务又可以继续分解,然后每个小任务分别计算出结果再合并起来,最后将汇总的结果作为 ...
- Java7任务并行执行神器:Fork&Join框架
原 Java7任务并行执行神器:Fork&Join框架 2018年01月12日 17:25:03 Java技术栈 阅读数:426 标签: JAVAFORKJOIN 更多 个人分类: Java ...
- 聊聊并发(八)——Fork/Join框架介绍
作者 方腾飞 发布于 2013年12月23日 | 被首富的“一个亿”刷屏?不如定个小目标,先把握住QCon上海的优惠吧!2 讨论 分享到:微博微信FacebookTwitter有道云笔记邮件分享 ...
- 转:聊聊并发(八)——Fork/Join框架介绍
1. 什么是Fork/Join框架 Fork/Join框架是Java7提供了的一个用于并行执行任务的框架, 是一个把大任务分割成若干个小任务,最终汇总每个小任务结果后得到大任务结果的框架. 我们再通过 ...
- Java并发编程--Fork/Join框架使用
上篇博客我们介绍了通过CyclicBarrier使线程同步,可是上述方法存在一个问题,那就是假设一个大任务跑了2个线程去完毕.假设线程2耗时比线程1多2倍.线程1完毕后必须等待线程2完毕.等待的过程线 ...
- 多线程(五) Fork/Join框架介绍及实例讲解
什么是Fork/Join框架 Fork/Join框架是Java7提供了的一个用于并行执行任务的框架, 是一个把大任务分割成若干个小任务,最终汇总每个小任务结果后得到大任务结果的框架. 我们再通过For ...
随机推荐
- Oracle使用虚拟表dual一次插入多条记录
从一个CSV文件中读取所有的数据,并且插入到一个Oracle数据库中,并且几分钟内完成,大约有60万条.网上有人说了,你可以循环insert然后插入几千条以后Commit一次,我靠,你自己试试看!!如 ...
- Weex-进阶笔记二
p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px Helvetica; color: #945200 } p.p2 { margin: 0.0p ...
- CentOS安装glibc-2.14,错误安装libc.so.6丢失急救办法
CentOS安装glibc-2.14,错误安装libc.so.6丢失急救办法 到http://ftp.gnu.org/gnu/glibc/下载glibc-2.14.tar.xz tar glibc ...
- 《学习的艺术》 (The Art of Learning)
这是我本科期间读过的的一本,个人感觉很有价值的书.当时刚刚失恋,正在思考人生,看这本书的时候,收获很多. 划小圈 (Making Smaller Circles)
- 修改虚拟机内容导致oracle不能启动
虚拟机内存目前设置为4G,想要改变成2G,数据库启动时导致报targetmomory错误,解决办法如下: 1.查看分配的memory_target和memory_max_target大小 SQL> ...
- logstash 输出到elasticsearch 自动建立index
由于es 单index 所能承受的数据量有限,之前情况是到400w数据300G左右的时候,整个数据的插入会变得特别慢(索引重建)甚至会导致集群之间的通信断开,于是我们采用每天一个index的方法来缓解 ...
- ubuntu 14.04 opencv2.4.13 安装
1.下载然后解压安装压缩包 unzip opencv-2.4.13.zip 2. 进入刚解压的文件夹,建立release文件夹 cd opencv-2.4.13 mkdir release 3. 安装 ...
- sublime 3 增加php开发插件
1.PHP语法自动完成插件 https://github.com/erichard/SublimePHPCompanion 2.ThinkPHP自动完成插件 https://github.com/ya ...
- 辽宁OI2016夏令营模拟T3-chess
放棋子(chess.pas/c/cpp)题目大意现在有一个 n*m 的棋盘,现在你需要在棋盘上摆放 2n 个棋子,要求满足如下条件:1. 每一列只能有一个棋子:2. 每一行的前 xi 个格子有一个棋子 ...
- [CSS备忘] css3零散
-webkit-overflow-scrolling:touch;下拉滚动回弹