当前位置:网站首页>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();
}
}
}
边栏推荐
- SQL implementation merges multiple rows of records into one row
- EasyExcel
- Practice of enterprise ab/testing platform
- Laravel Web框架
- php artisan
- Interface learning
- Jmeter+influxdb+grafana of performance tools to create visual real-time monitoring of pressure measurement -- problem record
- 10000小時定律不會讓你成為編程大師,但至少是個好的起點
- (翻译)异步编程:Async/Await在ASP.NET中的介绍
- DNS forward query:
猜你喜欢

HMS core helps baby bus show high-quality children's digital content to global developers

Inno setup production and installation package

EasyExcel

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

Liang Ning: 30 lectures on brain map notes for growth thinking

【类和对象】深入浅出类和对象

2022-06-23 VGMP-OSPF-域間安全策略-NAT策略(更新中)

Pytest -- write and manage test cases

Dbnet: real time scene text detection with differentiable binarization

Machine learning | simple but feature standardization methods that can improve the effect of the model (comparison and analysis of robustscaler, minmaxscaler, standardscaler)
随机推荐
Application scenarios of Catalan number
VMware virtual machine C disk expansion
CentOS switches and installs mysql5.7 and mysql8.0
How does the insurance company check hypertension?
Troubleshooting of high CPU load but low CPU usage
php安装swoole扩展
New knowledge! The virtual machine network card causes your DNS resolution to slow down
2022 East China Normal University postgraduate entrance examination machine test questions - detailed solution
Abstract learning
The 10000 hour rule won't make you a master programmer, but at least it's a good starting point
Jmeter+influxdb+grafana of performance tools to create visual real-time monitoring of pressure measurement -- problem record
MySQL mistakenly deleted the root account and failed to log in
每日刷题记录 (十一)
DNS forward query:
保险公司怎么查高血压?
Pytest -- write and manage test cases
Win 10 find the port and close the port
Practical plug-ins in idea
Personally design a highly concurrent seckill system
Shim and Polyfill in [concept collection]