计算10亿的数字求和
普通计算:
public static void main(String[] args) {
Long sum = 0l;
long start = System.currentTimeMillis();
for (long i = 1; i <= 10_0000_0000l; i++) {
sum += i;
}
long end =System.currentTimeMillis();
System.out.println(sum);
System.out.println((end - start));
}
耗时:5.5秒
ForkJoin计算:
ForkJoin的工具类(递归拆分任务,双向队列任务窃取)
public class ForkJoinDemo1 extends RecursiveTask<Long> {
private Long start;
private Long end;
//临界值
private Long temp=1000L;
public ForkJoinDemo1(Long start, Long end) {
this.start = start;
this.end = end;
}
@Override
protected Long compute() {
if ((end - start) < temp) {
Long sum = 0l;
for (Long i = start; i <= end; i++) {
sum += i;
}
return sum;
} else {
long middle = (start + end) / 2;
ForkJoinDemo1 task1= new ForkJoinDemo1(start, middle);
task1.fork();//拆封任务,吧任务压入线程队列
ForkJoinDemo1 task2= new ForkJoinDemo1(middle, end);
task2.fork();//拆封任务,吧任务压入线程队列
return task1.join() + task2.join();
}
}
}
调用
public class Test {
public static void main(String[] args) {
long start = System.currentTimeMillis();
ForkJoinPool forkJoinPool = new ForkJoinPool();
ForkJoinTask<Long> forkJoinDemo1 = new ForkJoinDemo1(0L, 10_0000_0000L);
ForkJoinTask<Long> submit = forkJoinPool.submit(forkJoinDemo1);
try {
System.out.println(submit.get());
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
}
long end =System.currentTimeMillis();
System.out.println((end - start));
}
}
耗时:10秒(可能是电脑的缘故 理论应该是比普通的快)
流式计算
public class Test{
public static void main(String[] args) {
long start = System.currentTimeMillis();
System.out.println(LongStream.rangeClosed(0L, 10_0000_0000L).parallel().reduce(0, Long::sum));
long end =System.currentTimeMillis();
System.out.println((end - start));
}
}
耗时:0.5秒