当前位置:网站首页>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();
}
}
}
边栏推荐
- MySQL installation
- 2021 year end summary
- The dynamic analysis and calculation of expressions are really delicious for flee
- Summary of the design and implementation of the weapon system similar to the paladin of vitality
- CentOS switches and installs mysql5.7 and mysql8.0
- 每日刷题记录 (十一)
- Know flex box
- DNS forward query:
- Modify MySQL password
- Win 10 find the port and close the port
猜你喜欢

Realize PDF to picture conversion with C #

Journal quotidien des questions (11)

Integration test practice (1) theoretical basis
![[classes and objects] explain classes and objects in simple terms](/img/41/250457530880dfe3728432c2ccd50b.png)
[classes and objects] explain classes and objects in simple terms
![[untitled]](/img/72/4ff9354634342580a135debb58b60e.jpg)
[untitled]
![[vscode - vehicle plug-in reports an error] cannot find module 'xxx' or its corresponding type declarations Vetur(2307)](/img/7f/3d6b6ea5319f7165e07baf0a78d318.jpg)
[vscode - vehicle plug-in reports an error] cannot find module 'xxx' or its corresponding type declarations Vetur(2307)

The dynamic analysis and calculation of expressions are really delicious for flee

多个全局异常处理类,怎么规定执行顺序

On the practice of performance optimization and stability guarantee

golang操作redis:写入、读取hash类型数据
随机推荐
Basic teaching of crawler code
2022 - 06 - 23 vgmp - OSPF - Inter - Domain Security Policy - nat Policy (Update)
Gridome + strapi + vercel + PM2 deployment case of [static site (3)]
Inno Setup 制作安装包
Practical plug-ins in idea
Software testing learning - the next day
Thoughts in Starbucks
Notes on the core knowledge of Domain Driven Design DDD
Journal quotidien des questions (11)
[vscode - vehicle plug-in reports an error] cannot find module 'xxx' or its corresponding type declarations Vetur(2307)
[untitled]
Daily question brushing record (11)
2022-06-23 VGMP-OSPF-域间安全策略-NAT策略(更新中)
Software testing assignment - the next day
Ruoyi interface permission verification
How to plan well?
CentOS php7.3 installing redis extensions
Simple understanding of bubble sorting
[Code] occasionally take values, judge blanks, look up tables, verify, etc
Laravel Web框架