当前位置:网站首页>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();
}
}
}
边栏推荐
- IC_ EDA_ All virtual machine (rich Edition): questasim, vivado, VCs, Verdi, DC, Pt, spyglass, icc2, synthesize, innovative, ic617, mmsim, process library
- Daily question brushing record (11)
- POI excel percentage
- JMeter test result output
- 服务器如何设置多界面和装IIS呢?甜甜给你解答!
- Thoughts in Starbucks
- Hands on redis master-slave replication, sentinel master-slave switching, cluster sharding
- mongodb
- golang操作redis:写入、读取kv数据
- C2338 Cannot format an argument. To make type T formattable provide a formatter<T> specialization:
猜你喜欢
Notes on the core knowledge of Domain Driven Design DDD
Jenkins
golang操作redis:写入、读取kv数据
Software testing learning - day 3
EasyExcel
Software testing learning - the next day
Winter vacation work of software engineering practice
Yolov3 learning notes
In depth analysis of reentrantlock fair lock and unfair lock source code implementation
Software testing assignment - the next day
随机推荐
PHP install the spool extension
golang操作redis:写入、读取hash类型数据
【类和对象】深入浅出类和对象
Winter vacation work of software engineering practice
卡特兰数(Catalan)的应用场景
Laravel Web Framework
golang操作redis:写入、读取kv数据
[open source project recommendation colugomum] this group of undergraduates open source retail industry solutions based on the domestic deep learning framework paddlepadddle
PHP install composer
php artisan
These two mosquito repellent ingredients are harmful to babies. Families with babies should pay attention to choosing mosquito repellent products
JMeter JSON extractor extracts two parameters at the same time
Dbnet: real time scene text detection with differentiable binarization
MySQL mistakenly deleted the root account and failed to log in
【code】偶尔取值、判空、查表、验证等
DNS forward query:
Create your own deep learning environment with CONDA
Jmeter+influxdb+grafana of performance tools to create visual real-time monitoring of pressure measurement -- problem record
How to plan well?
Understand software testing