当前位置:网站首页>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();
}
}
}
边栏推荐
- How to migrate or replicate VMware virtual machine systems
- golang操作redis:写入、读取hash类型数据
- Setting up the development environment of dataworks custom function
- Search engine Bing Bing advanced search skills
- Personally design a highly concurrent seckill system
- Thoughts in Starbucks
- Climb movie paradise 2021 hot
- [Code] if (list! = null & list. Size() > 0) optimization, set empty judgment implementation method
- Error c2017: illegal escape sequence
- Reading notes of "learn to ask questions"
猜你喜欢
dataworks自定义函数开发环境搭建
On the practice of performance optimization and stability guarantee
New knowledge! The virtual machine network card causes your DNS resolution to slow down
POI excel percentage
每日刷題記錄 (十一)
The pressure of large institutions in the bear market has doubled. Will the giant whales such as gray scale, tether and micro strategy become 'giant thunder'?
MySQL installation
[Fiddler problem] solve the problem about Fiddler's packet capturing. After the mobile network is configured with an agent, it cannot access the Internet
vmware虚拟机C盘扩容
IC_EDA_ALL虚拟机(丰富版):questasim、vivado、vcs、verdi、dc、pt、spyglass、icc2、synplify、INCISIVE、IC617、MMSIM、工艺库
随机推荐
Resttemplate configuration use
centos php7.3安装redis扩展
Code management tools
每日刷题记录 (十一)
Stream stream
instanceof
UTC时间、GMT时间、CST时间
10000小時定律不會讓你成為編程大師,但至少是個好的起點
[attribute comparison] defer and async
How to plan well?
Jmeter+influxdb+grafana of performance tools to create visual real-time monitoring of pressure measurement -- problem record
MySQL transaction rollback, error points record
Upgrade CentOS php7.2.24 to php7.3
The difference between CONDA and pip
golang操作redis:写入、读取hash类型数据
Arctic code vault contributor
MySQL installation
C2338 Cannot format an argument. To make type T formattable provide a formatter<T> specialization:
EasyExcel
Hands on redis master-slave replication, sentinel master-slave switching, cluster sharding