JDK 7 中的 Fork/Join 模式 简单入门
2016-01-12 18:25
561 查看
任务类:
@SuppressWarnings("serial")
public class MyTask extends RecursiveTask<Integer> {
List<Integer> i;
public MyTask(List<Integer> list) {
this.i = list;
}
@Override
protected Integer compute() {
int res = Integer.MIN_VALUE;
if (i.size() > 2) {
MyTask l = new MyTask(i.subList(0, i.size()/2) );
MyTask r = new MyTask(i.subList(i.size()/2, i.size()));
l.fork();
r.fork();
res = Math.max(l.join(), r.join());
}else{
if(i.size() == 2){
System.out.println("compare " + i.get(0) + " and " + i.get(1));
res = Math.max(i.get(0), i.get(1)) ;
System.out.println("return "+res);
}else{
System.out.println("only one");
res = i.get(0);
System.out.println("return "+res);
}
}
return res;
}
}
启动任务:
public class MainTest {
public static void main(String[] args) {
ArrayList<Integer> l = new ArrayList<Integer>();
for(int i=0;i<1000;i++){
l.add((int)(Math.random() * 1000));
}
//System.out.println(l);
MyTask mt = new MyTask(l);
ForkJoinPool forkJoinPool = new ForkJoinPool();
Future<Integer> result = forkJoinPool.submit(mt);
try {
System.out.println("max is " + result.get());
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
}
forkJoinPool.shutdown();
}
}
实际过程中一个值得注意的地方:如果任务启动的线程数过多,会出现内存溢出:java.lang.OutOfMemoryError: unable to create new native thread。
默认jvm设置下,最多本地线程数只有2300个左右
经过简单的ivm参数设置-xss设为64k时线程数达到4630个左右。
@SuppressWarnings("serial")
public class MyTask extends RecursiveTask<Integer> {
List<Integer> i;
public MyTask(List<Integer> list) {
this.i = list;
}
@Override
protected Integer compute() {
int res = Integer.MIN_VALUE;
if (i.size() > 2) {
MyTask l = new MyTask(i.subList(0, i.size()/2) );
MyTask r = new MyTask(i.subList(i.size()/2, i.size()));
l.fork();
r.fork();
res = Math.max(l.join(), r.join());
}else{
if(i.size() == 2){
System.out.println("compare " + i.get(0) + " and " + i.get(1));
res = Math.max(i.get(0), i.get(1)) ;
System.out.println("return "+res);
}else{
System.out.println("only one");
res = i.get(0);
System.out.println("return "+res);
}
}
return res;
}
}
启动任务:
public class MainTest {
public static void main(String[] args) {
ArrayList<Integer> l = new ArrayList<Integer>();
for(int i=0;i<1000;i++){
l.add((int)(Math.random() * 1000));
}
//System.out.println(l);
MyTask mt = new MyTask(l);
ForkJoinPool forkJoinPool = new ForkJoinPool();
Future<Integer> result = forkJoinPool.submit(mt);
try {
System.out.println("max is " + result.get());
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
}
forkJoinPool.shutdown();
}
}
实际过程中一个值得注意的地方:如果任务启动的线程数过多,会出现内存溢出:java.lang.OutOfMemoryError: unable to create new native thread。
默认jvm设置下,最多本地线程数只有2300个左右
经过简单的ivm参数设置-xss设为64k时线程数达到4630个左右。
相关文章推荐
- Spring 笔记
- SpringMVC搭建
- struts2中s:iterator 标签的使用详解
- eclipse 安装反编译插件
- Java 8 学习–lambda表达式
- Java注解(1)-基础
- 40个Java多线程问题总结
- java.util.Collection体系源码解读<二>Collection接口源码
- Java-Map
- springmvc开发--IOC和AOP
- Java-Map
- Java-Map
- spring容器加载完毕做一件事情(利用ContextRefreshedEvent事件)
- Java [Leetcode 110]Balanced Binary Tree
- 详解Spring MVC 4常用的那些注解
- 查看java类是从哪个包加载
- 在spring MVC的controller中获取ServletConfig
- SSL介绍与Java实例
- Selenium+java环境搭建
- Android with Eclipse - Waiting for HOME ('android.process.acore') to be launched?