首页
统计
关于
Search
1
Sealos3.0离线部署K8s集群
1,085 阅读
2
类的加载
741 阅读
3
Spring Cloud OAuth2.0
726 阅读
4
SpringBoot自动装配原理
691 阅读
5
集合不安全问题
586 阅读
笔记
Java
多线程
注解和反射
JVM
JUC
设计模式
Mybatis
Spring
SpringMVC
SpringBoot
MyBatis-Plus
Elastic Search
微服务
Dubbo
Zookeeper
SpringCloud
Nacos
Sentinel
数据库
MySQL
Oracle
PostgreSQL
Redis
MongoDB
工作流
Activiti7
Camunda
消息队列
RabbitMQ
前端
HTML5
CSS
CSS3
JavaScript
jQuery
Vue2
Vue3
Linux
容器
Docker
Kubernetes
Python
FastApi
登录
Search
标签搜索
Java
CSS
mysql
RabbitMQ
JavaScript
Redis
JVM
Mybatis-Plus
Camunda
多线程
CSS3
Python
Spring Cloud
注解和反射
Activiti
工作流
SpringBoot
Mybatis
Spring
html5
蘇阿細
累计撰写
389
篇文章
累计收到
4
条评论
首页
栏目
笔记
Java
多线程
注解和反射
JVM
JUC
设计模式
Mybatis
Spring
SpringMVC
SpringBoot
MyBatis-Plus
Elastic Search
微服务
Dubbo
Zookeeper
SpringCloud
Nacos
Sentinel
数据库
MySQL
Oracle
PostgreSQL
Redis
MongoDB
工作流
Activiti7
Camunda
消息队列
RabbitMQ
前端
HTML5
CSS
CSS3
JavaScript
jQuery
Vue2
Vue3
Linux
容器
Docker
Kubernetes
Python
FastApi
页面
统计
关于
搜索到
1
篇与
的结果
2021-02-16
ForkJoin
ForkJoinForkJoin图片来源:狂神说JavaForkJoin 任务窃取这个任务中维护的都是双端队列B的任务执行完了,去帮助A执行图片来源:狂神说Javapackage com.sw.forkjoin; import java.util.concurrent.RecursiveTask; /** * @Author suaxi * @Date 2021/2/16 17:24 * 1、通过forkjoinPool来执行 * 2、计算任务forkjoinPool.execute(ForkJoinTask task) * 3、计算类要继承 ForkJoinTask */ public class ForkJoinTest extends RecursiveTask<Long> { private Long start; private Long end; //临界值 private Long temp = 1000L; public ForkJoinTest(Long start, Long end) { this.start = start; this.end = end; } //计算任务 @Override protected Long compute() { if ((end-start)<temp){ Long sum = 0L; for (Long i = start; i <= end; i++) { sum += i; } return sum; }else { //forkjoin递归 long middle = (start + end)/2; ForkJoinTest task1 = new ForkJoinTest(start, middle); task1.fork(); //拆分任务,把任务压入线程队列 ForkJoinTest task2 = new ForkJoinTest(middle+1, end); task2.fork(); //同理 return task1.join() + task2.join(); } } } 测试类:package com.sw.forkjoin; import java.util.concurrent.ExecutionException; import java.util.concurrent.ForkJoinPool; import java.util.concurrent.ForkJoinTask; import java.util.stream.LongStream; /** * @Author suaxi * @Date 2021/2/16 17:37 */ public class Test { public static void main(String[] args) throws ExecutionException, InterruptedException { //test01(); //计算时间=7166 //test02(); //计算时间=5244 test03(); //计算时间=312 } //常规计算方法 public static void test01(){ Long sum =0L; long start = System.currentTimeMillis(); for (Long i = 1L; i < 10_0000_0000; i++) { sum += i; } long end = System.currentTimeMillis(); System.out.println("sum="+sum+" 计算时间="+ (end - start)); } //ForkJoin public static void test02() throws ExecutionException, InterruptedException { long start = System.currentTimeMillis(); //1、通过ForkJoinPool执行 ForkJoinPool forkJoinPool = new ForkJoinPool(); ForkJoinTask<Long> task = new ForkJoinTest(0L, 10_0000_0000L); //2、计算任务forkjoinPool.execute(ForkJoinTask task) ForkJoinTask<Long> submit = forkJoinPool.submit(task); Long sum = submit.get(); long end = System.currentTimeMillis(); System.out.println("sum="+sum+" 计算时间="+ (end - start)); } //Stream并行流 public static void test03(){ long start = System.currentTimeMillis(); //parallel并行 Long sum = LongStream.rangeClosed(0L,10_0000_0000L).parallel().reduce(0,Long::sum); long end = System.currentTimeMillis(); System.out.println("sum="+sum+" 计算时间="+ (end - start)); } }
2021年02月16日
45 阅读
0 评论
0 点赞