当前位置:网站首页>Forkjoin and stream flow test
Forkjoin and stream flow test
2022-07-01 06:20:00 【qq_ fifty-two million twenty-five thousand two hundred and eight】
What is? ForkJoin?
ForkJoin stay JDK 1.7 , Execute tasks in parallel ! Increase of efficiency . Large amount of data ! big data :Map Reduce ( Split big tasks into small ones )
ForkJoin characteristic : Job theft
ForkJoinPool internals - Job theft
stay ForkJoinPool in , Each worker thread in the thread pool (ForkJoinWorkerThread) All correspond to a task queue (WorkQueue), The worker thread prioritizes tasks from its own queue (LIFO or FIFO The order , Parameters mode decision ), And then to FIFO Randomly steal tasks from other queues in the order of .
import java.util.concurrent.RecursiveTask;
/** * The task of summation calculation ! * 3000 6000(ForkJoin) 9000(Stream Parallel flow ) * // How to use forkjoin * // 1、forkjoinPool To execute through it * // 2、 Calculation task forkjoinPool.execute(ForkJoinTask task) * // 3. The calculation class should inherit ForkJoinTask */
public class ForkJoinDemo extends RecursiveTask<Long> {
private Long start;
private Long end;
// critical value
private Long tmp = 10000L;
public ForkJoinDemo(Long start, Long end) {
this.start = start;
this.end = end;
}
// computing method
@Override
protected Long compute() {
if ((end-start)<tmp){
Long sum = 0L;
for (Long i = start; i <= end; i++) {
sum += i;
}
return sum;
}else {
// forkjoin recursive
long middle = (start + end) / 2; // In the middle
ForkJoinDemo task1 = new ForkJoinDemo(start, middle);
task1.fork(); // Split the task , Push the task into the thread queue
ForkJoinDemo task2 = new ForkJoinDemo(middle+1, end);
task2.fork(); // Split the task , Push the task into the thread queue
return task1.join() + task2.join();
}
}
}
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ForkJoinPool;
import java.util.concurrent.ForkJoinTask;
import java.util.stream.LongStream;
public class Main {
public static void main(String[] args) throws ExecutionException, InterruptedException {
// test1();
// test2();
test3();
}
// Ordinary programmers
public static void test1(){
Long sum = 0L;
long start = System.currentTimeMillis();
for (Long i = 1L; i <= 10_0000_0000; i++) {
sum += i;
}
long end = System.currentTimeMillis();
System.out.println("sum="+sum+" Time :"+(end-start));
}
// Will use ForkJoin
public static void test2() throws ExecutionException, InterruptedException {
long start = System.currentTimeMillis();
ForkJoinPool pool = new ForkJoinPool();
ForkJoinTask<Long> task = new ForkJoinDemo(0L,10_0000_0000L);
ForkJoinTask<Long> submit = pool.submit(task);
Long sum = submit.get();
long end = System.currentTimeMillis();
System.out.println("sum="+sum+" Time :"+(end-start));
}
// Stream Parallel flow ():range (]:rangeClosed
public static void test3(){
long start = System.currentTimeMillis();
long sum = LongStream.rangeClosed(0L,10_0000_0000L).parallel().reduce(0,Long::sum);
long end = System.currentTimeMillis();
System.out.println("sum="+sum +" Time :"+(end-start));
}
}
边栏推荐
- [summary of knowledge points] chi square distribution, t distribution, F distribution
- MySQL里记录货币
- π disk, turning your computer into a personal private cloud
- 2022 年面向初学者的 10 大免费 3D 建模软件
- 【文件系统】如何在ubi之上运行squashfs
- 讓田頭村變甜頭村的特色農產品是仙景芋還是白菜
- 异常检测方法梳理,看这篇就够了!
- Excel visualization
- 【ManageEngine】终端管理系统,助力华盛证券数字化转型
- 69 Cesium代码datasource加载geojson
猜你喜欢

【ManageEngine】如何实现网络自动化运维

高阶-二叉平衡树

Ant new village is one of the special agricultural products that make Tiantou village in Guankou Town, Xiamen become Tiantou village
![[self use of advanced mathematics in postgraduate entrance examination] advanced mathematics Chapter 1 thinking map in basic stage](/img/54/f187e22ad69f3985d30376bad1fa03.png)
[self use of advanced mathematics in postgraduate entrance examination] advanced mathematics Chapter 1 thinking map in basic stage

Freeswitch dial the extension number

To sort out the anomaly detection methods, just read this article!

68 Cesium代码datasource加载czml

记磁盘扇区损坏导致的Mysql故障排查
![[file system] how to run squashfs on UBI](/img/d7/a4769420c510c47f3c2a615b514a8e.png)
[file system] how to run squashfs on UBI

【ITSM】什么是ITSM,IT部门为什么需要ITSM
随机推荐
HDU - 1501 Zipper(记忆化深搜)
Arcserver password reset (account cannot be reset)
The row and column numbers of each pixel of multi-source grid data in the same area are the same, that is, the number of rows and columns are the same, and the pixel size is the same
MySQL里记录货币
OpenGL es: (2) relationship between OpenGL es, EGL and glsl
【ManageEngine卓豪 】助力世界顶尖音乐学院--茱莉亚学院,提升终端安全
Dongle data collection
异常检测方法梳理,看这篇就够了!
FPGA - 7 Series FPGA internal structure clocking-01-clock Architecture Overview
kubeadm搭建kubenetes 集群(个人学习版)
做技术,自信不可或缺
PLA not pasted on the bed: 6 simple solutions
C# ManualResetEvent 类的理解
HDU - 1501 zipper (memory deep search)
让田头村变甜头村的特色农产品是仙景芋还是白菜
UOW of dev XPO comparison
Servlet
Recueillir des trésors dans le palais souterrain (recherche de mémoire profonde)
Multi label lsml for essay learning records
ArcServer密码重置(账号不可以重置)