当前位置:网站首页>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));
}
}
边栏推荐
- 可动的机械挂钟
- Oracle sequence + trigger
- Tidb single machine simulation deployment production environment cluster (closed pit practice, personal test is effective)
- Solve the problem of garbled files uploaded by Kirin v10
- 浅谈SIEM
- 【LeetCode】Day91-存在重复元素
- SystemVerilog learning-08-random constraints and thread control
- 地宮取寶(記憶化深搜)
- Pit of kotlin bit operation (bytes[i] and 0xff error)
- Thesis learning record essay multi label lift
猜你喜欢

无限水平大理石游戏

Multi label lsml for essay learning records
![[note] e-commerce order data analysis practice](/img/03/367756437be947b5b995d5f7f55236.png)
[note] e-commerce order data analysis practice

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

【企业数据安全】升级备份策略 保障企业数据安全

图片服务器项目测试

FPGA - 7 Series FPGA internal structure clocking-01-clock Architecture Overview

【网络安全工具】USB控制软件有什么用

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

Essay learning record essay multi label Global
随机推荐
Top 10 Free 3D modeling software for beginners in 2022
SOE空间分析服务器 MySQL以及PostGres的地理空间库PostGIS防注入攻击
SOE spatial analysis server MySQL and PostGIS geospatial database of Postgres anti injection attack
ArcServer密码重置(账号不可以重置)
高阶-二叉平衡树
Elements of database ER diagram
Arcserver password reset (account cannot be reset)
Database problems, how to optimize Oracle SQL query statements faster and more efficient
Stack Title: parsing Boolean expressions
Transformer le village de tiantou en un village de betteraves sucrières
PLA不粘貼在床上:6個簡單的解决方案
Restframework-simplejwt rewrite authentication mechanism
FPGA - 7 Series FPGA internal structure clocking-01-clock Architecture Overview
What if the data in the cloud disk is harmonious?
可动的机械挂钟
【ManageEngine卓豪】移动终端管理解决方案,助力中州航空产业数字化转型
Using Baidu map to query national subway lines
让厦门灌口镇田头村变“甜头”村的特色农产品之一是
Thesis learning record essay multi label lift
做技术,自信不可或缺