当前位置:网站首页>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();
}
}
}
边栏推荐
- Software testing assignment - day 1
- IC_EDA_ALL虚拟机(丰富版):questasim、vivado、vcs、verdi、dc、pt、spyglass、icc2、synplify、INCISIVE、IC617、MMSIM、工艺库
- (翻译)异步编程:Async/Await在ASP.NET中的介绍
- Machine learning | simple but feature standardization methods that can improve the effect of the model (comparison and analysis of robustscaler, minmaxscaler, standardscaler)
- Summary of remote connection of MySQL
- Unit test notes
- Notes on the core knowledge of Domain Driven Design DDD
- Stream stream
- The 10000 hour rule won't make you a master programmer, but at least it's a good starting point
- The list of "I'm crazy about open source" was released in the first week, with 160 developers on the list
猜你喜欢

New knowledge! The virtual machine network card causes your DNS resolution to slow down

Jenkins

Pits encountered in the use of El checkbox group

Application scenarios of Catalan number

Software testing learning - the next day

(翻译)异步编程:Async/Await在ASP.NET中的介绍

Setting up the development environment of dataworks custom function

10000小时定律不会让你成为编程大师,但至少是个好的起点

Selenium - by changing the window size, the width, height and length of different models will be different

Practical plug-ins in idea
随机推荐
熊市里的大机构压力倍增,灰度、Tether、微策略等巨鲸会不会成为'巨雷'?
Error c2017: illegal escape sequence
Software testing assignment - the next day
[day15] introduce the features, advantages and disadvantages of promise, and how to implement it internally. Implement promise by hand
The essence of interview
JMeter JSON extractor extracts two parameters at the same time
Crontab scheduled task
Selenium - by changing the window size, the width, height and length of different models will be different
mongodb
[LeetCode]404. Sum of left leaves
Resttemplate configuration use
Laravel框架 踩坑(一)
Climb movie paradise 2021 hot
[untitled]
New knowledge! The virtual machine network card causes your DNS resolution to slow down
Pytest -- write and manage test cases
(翻译)异步编程:Async/Await在ASP.NET中的介绍
[classes and objects] explain classes and objects in simple terms
Realize PDF to picture conversion with C #
dataworks自定義函數開發環境搭建