forkjoin框架疑问记录
今天在看jdk1.7的forkjoin框架时候,里面有个例子如下:
product类:
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;
}
}
ProductListGenerator类:
public class ProductListGenerator {
public List<Product> generate(int size){
List<Product> ret= new ArrayList<>();
for (int i = 0; i<size;i++){
Product product = new Product();
product.setName("product" + i);
product.setPrice(10);
ret.add(product);
}
return ret;
}
}
Task类:
public class Task extends RecursiveAction {
private int first;
private int last;
private double increment;
private List<Product> productList;
public Task(int first, int last, double increment, List<Product> productList) {
this.first = first;
this.last = last;
this.increment = increment;
this.productList = productList;
}
@Override
protected void compute() {
if (last - first <9){
updatePrices();
}else {
int middle = (last+first)/2;
//System.out.printf("Task:Pending tasks:%s\n",getQueuedTaskCount());
Task t1 = new Task(first,middle+1,increment,productList);
Task t2 = new Task(middle+1,last,increment,productList);
invokeAll(t1,t2);
}
}
private void updatePrices(){
for(int i = first;i<last;i++){
Product product = productList.get(i);
product.setPrice(product.getPrice()*(1+increment));
}
}
}
main方法:
public class Main {
public static void main(String[] args) {
ProductListGenerator generator = new ProductListGenerator();
Long startTime = System.currentTimeMillis();
List<Product> products = generator.generate(10000000);
Task task = new Task(0,products.size(),0.20,products);
ForkJoinPool forkJoinPool = new ForkJoinPool();
forkJoinPool.execute(task);
do {
System.out.printf("Main:Thread count:%d\n",forkJoinPool.getActiveThreadCount());
System.out.printf("Main:Thread steal:%d\n",forkJoinPool.getStealCount());
System.out.printf("Main:Parallelism:%d\n",forkJoinPool.getParallelism());
try {
// TimeUnit.MILLISECONDS.sleep(5);
} catch (InterruptedException e) {
e.printStackTrace();
}
}while (!task.isDone());
System.out.println("========");
forkJoinPool.shutdown();
long endTime = System.currentTimeMillis();
long time = endTime-startTime;
System.out.println("在内存中运算时间:" + time + "毫秒");
if (task.isCompletedNormally()){
System.out.printf("Main:The proccess has completed normally.\n");
}
for (int i =0;i<products.size();i++){
Product product = products.get(i);
if(product.getPrice() != 12){
System.out.printf("Product %s : %f\n",product.getName(),product.getPrice());
}
}
System.out.printf("Main:End of the program. \n");
}
}
这样是没问题的,1千万条数据运行大概需要15000多毫秒.然而自己实现,不用实现RecursiveAction的话只要5000毫秒左右:代码
long starT1 = System.currentTimeMillis();
List<Product> list = new ArrayList<>();
for(int i =0;i<10000000;i++){
Product product = new Product();
product.setName("product" + i);
product.setPrice(10);
list.add(product);
}
for(int i = 0;i<list.size();i++){
Product product = list.get(i);
product.setPrice(product.getPrice()*(1+0.2));
}
long endTimeT2 =System.currentTimeMillis();
long t = endTimeT2 -starT1;
System.out.println("单线程1000万数据时间:" + t + "毫秒");
就有点不明白了,就算是因为在task里面有构造方法以及因为判断影响,但是这样多线程是为了什么那? 还有 我把ArrayList修改为Actor,这样也是差不多的结果。。。 没明白fork/join框架的devide方法 究竟有什么好处?
后来明白了:demo里面的 属于计算密集型,线程数目应该适当小些。因为有线程的来回切换,导致时间比单线程要慢些,如果在单线程加上休眠1毫秒,会发现那个速度特别慢了;而如果是IO密集型,比如读取文件、数据库连接、网络通讯,线程数适当大些。
forkjoin框架疑问记录的更多相关文章
- Java并发包线程池之ForkJoinPool即ForkJoin框架(一)
前言 这是Java并发包提供的最后一个线程池实现,也是最复杂的一个线程池.针对这一部分的代码太复杂,由于目前理解有限,只做简单介绍.通常大家说的Fork/Join框架其实就是指由ForkJoinPoo ...
- ForkJoin框架
1. 什么是Fork/Join框架 Fork/Join框架是Java7提供了的一个用于并行执行任务的框架, 是一个把大任务分割成若干个小任务,最终汇总每个小任务结果后得到大任务结果的框架. 我们再通过 ...
- java fork-join框架应用和分析
http://shmilyaw-hotmail-com.iteye.com/blog/1897636 java fork-join框架应用和分析 博客分类: concurrency multithre ...
- Java并发编程原理与实战三十二:ForkJoin框架详解
1.Fork/Join框架有什么用呢? ------->Fork使用来切分任务,Join是用来汇总结果.举个简单的栗子:任务是1+2+3+...+100这个任务(当然这个任务的结果有好的算法去做 ...
- Java7 Fork-Join 框架:任务切分,并行处理
概要 现代的计算机已经向多CPU方向发展,即使是普通的PC,甚至现在的智能手机.多核处理器已被广泛应用.在未来,处理器的核心数将会发展的越来越多.虽然硬件上的多核CPU已经十分成熟,但是很多应用程序并 ...
- Java--8--新特性--串并行流与ForkJoin框架
并行流就是把一个内容分成多个数据块,并用不同的线程分别处理每个数据块的流.穿行流则相反,并行流的底层其实就是ForkJoin框架的一个实现. 那么先了解一下ForkJoin框架吧. Fork/Join ...
- 4.VUE前端框架学习记录四:Vue组件化编码2
VUE前端框架学习记录四:Vue组件化编码2文字信息没办法描述清楚,主要看编码Demo里面,有附带完整的代码下载地址,有需要的同学到脑图里面自取.脑图地址http://naotu.baidu.com/ ...
- 3.VUE前端框架学习记录三:Vue组件化编码1
VUE前端框架学习记录三:Vue组件化编码1文字信息没办法描述清楚,主要看编码Demo里面,有附带完整的代码下载地址,有需要的同学到脑图里面自取.脑图地址http://naotu.baidu.com/ ...
- 2.VUE前端框架学习记录二
VUE前端框架学习记录二:Vue核心基础2(完结)文字信息没办法描述清楚,主要看编码实战里面,有附带有一个完整可用的Html页面,有需要的同学到脑图里面自取.脑图地址http://naotu.baid ...
随机推荐
- C#常用总结《一》
集合类常用: List<T> 泛型集合 Dictionary<key,value> 字典集合 文件读取: FileStream :对各种文件读写,字节处理更好 StreamR ...
- CSS外边距合并&块格式上下文
前言问题Margin Collapsing 外边距合并Block Formatting Context 块格式化上下文解决方案参考 前言 之前在前端开发的过程中,都没有遇到外边距合并的问题(其实是因为 ...
- Mac下安装JDK(Mac 10.12)
1.到官网http://www.oracle.com/technetwork/java/javase/downloads/index.html下载JDK 2.安装 打开dmg包 3.测试 在终端上输入 ...
- 【ORACLE】ID 2299494.1 安装Oracle 11g 86%报错:Error in invoking target 'agent nmhs' of makefile
参考: ID 2299494.1 In this Document Symptoms Changes Cause Solution References APPLIES TO: O ...
- hibernate核心开发接口_Configuration
AnnotationConfiguration继承自Configuration,这里以AnnotationConfiguration为例: new AnnotationConfiguration(). ...
- EasyMock set方法报错: java.lang.AssertionError
有返回值的方法没问题, 直接andReturn就行了. EasyMock.expect(info.getWebTitle()).andReturn(StringUtils.EMPTY).anyTime ...
- javascript中的function 函数名(){} 和 函数名:function(){}有什么不同
function functionName(){};这是定义一个函数 functionName:function(){};是设置一个对象的方法. 下面举一个例子: <html> <h ...
- canvas猜数游戏
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- Entity Framework 6.0 Code First(转)
源自:http://www.cnblogs.com/panchunting/tag/Code%20First/ 1 Conventions 2 Custom Conventions 3 Data An ...
- UUID 压缩为22位
public class Generator { private static char[] BASE64 = "abcdefghijklmnopqrstuvwxyz_ABCDEFGHIJK ...