当前位置:网站首页>JUC forkjoinpool branch merge framework - work theft
JUC forkjoinpool branch merge framework - work theft
2022-07-03 06:58:00 【W_ Meng_ H】

package org.meng.juc;
import java.time.Duration;
import java.time.Instant;
import java.util.concurrent.ForkJoinPool;
import java.util.concurrent.ForkJoinTask;
import java.util.concurrent.RecursiveTask;
import java.util.stream.LongStream;
import org.junit.Test;
public class TestForkJoinPool {
public static void main(String[] args) {
Instant start = Instant.now();
ForkJoinPool pool = new ForkJoinPool();
ForkJoinTask<Long> task = new ForkJoinSumCalculate(0L, 50000000000L);
Long sum = pool.invoke(task);
System.out.println(sum);
Instant end = Instant.now();
System.out.println(" Time consuming :" + Duration.between(start, end).toMillis());//166-1996-10590
}
@Test
public void test1(){
Instant start = Instant.now();
long sum = 0L;
for (long i = 0L; i <= 50000000000L; i++) {
sum += i;
}
System.out.println(sum);
Instant end = Instant.now();
System.out.println(" Time consuming :" + Duration.between(start, end).toMillis());//35-3142-15704
}
//java8 New characteristics
@Test
public void test2(){
Instant start = Instant.now();
Long sum = LongStream.rangeClosed(0L, 50000000000L)
.parallel()
.reduce(0L, Long::sum);
System.out.println(sum);
Instant end = Instant.now();
System.out.println(" Time consuming :" + Duration.between(start, end).toMillis());//1536-8118
}
}
class ForkJoinSumCalculate extends RecursiveTask<Long>{
/**
*
*/
private static final long serialVersionUID = -259195479995561737L;
private long start;
private long end;
private static final long THURSHOLD = 10000L; // critical value
public ForkJoinSumCalculate(long start, long end) {
this.start = start;
this.end = end;
}
@Override
protected Long compute() {
long length = end - start;
if(length <= THURSHOLD){
long sum = 0L;
for (long i = start; i <= end; i++) {
sum += i;
}
return sum;
}else{
long middle = (start + end) / 2;
ForkJoinSumCalculate left = new ForkJoinSumCalculate(start, middle);
left.fork(); // To break up , At the same time, press into the thread queue
ForkJoinSumCalculate right = new ForkJoinSumCalculate(middle+1, end);
right.fork(); //
return left.join() + right.join();
}
}
}
边栏推荐
- Inno Setup 制作安装包
- Reading notes of "learn to ask questions"
- Use the jvisualvm tool ----- tocmat to start JMX monitoring
- Selenium - by changing the window size, the width, height and length of different models will be different
- Integration test practice (1) theoretical basis
- Redis command
- Inno setup production and installation package
- How can the server set up multiple interfaces and install IIS? Tiantian gives you the answer!
- Jenkins
- centos php7.3安装redis扩展
猜你喜欢

Yolov2 learning and summary

Golang operation redis: write and read kV data

MySQL installation

如何迁移或复制VMware虚拟机系统

The pressure of large institutions in the bear market has doubled. Will the giant whales such as gray scale, tether and micro strategy become 'giant thunder'?

Yolov1 learning notes

Create your own deep learning environment with CONDA

Pits encountered in the use of El checkbox group

IC_ EDA_ All virtual machine (rich Edition): questasim, vivado, VCs, Verdi, DC, Pt, spyglass, icc2, synthesize, innovative, ic617, mmsim, process library

Software testing assignment - day 1
随机推荐
如何迁移或复制VMware虚拟机系统
10000小時定律不會讓你成為編程大師,但至少是個好的起點
【类和对象】深入浅出类和对象
Inno setup production and installation package
Software testing learning - day one
利用C#实现Pdf转图片
error C2017: 非法的转义序列
Abstract learning
【无标题】
每日刷题记录 (十一)
C2338 Cannot format an argument. To make type T formattable provide a formatter<T> specialization:
[attribute comparison] defer and async
What are the characteristics and functions of the scientific thinking mode of mechanical view and system view
Practice of enterprise ab/testing platform
Summary of the design and implementation of the weapon system similar to the paladin of vitality
Machine learning | simple but feature standardization methods that can improve the effect of the model (comparison and analysis of robustscaler, minmaxscaler, standardscaler)
Jenkins
爬虫代码基础教学
[LeetCode]404. Sum of left leaves
2022-06-23 VGMP-OSPF-域间安全策略-NAT策略(更新中)