当前位置: 首页 > news >正文

古镇网站建设制作石家庄关键词排名提升

古镇网站建设制作,石家庄关键词排名提升,网站悬浮广告素材,无极任务平台还在用 for 循环计算两个数之间所有数的和吗?下面提供两种新方法! 1. ForkJoin 1.1 背景 要知道,在一个方法中,如果没有做特殊的处理,那么在方法开始到结束使用的都是同一个线程,无论你的业务有多复杂 那…

还在用 for 循环计算两个数之间所有数的和吗?下面提供两种新方法!

1. ForkJoin

1.1 背景

要知道,在一个方法中,如果没有做特殊的处理,那么在方法开始到结束使用的都是同一个线程,无论你的业务有多复杂
那么就有人在想,能不能在一个方法里使用多个线程来完成一个复杂的业务?

ForkJoin 拆分合并
在一定程度上满足了上述的场景,使用 ForkJoin 后,可以根据自己的需要将一个任务拆分为多个任务;当多个任务都执行完后,再将结果汇总返回

1.2 实例

比如:我现在要计算 1~1000000000 之间所有数的和

继承 RecursiveTask 并行任务类,编写对应的计算方法

public class ForkJoinDemo extends RecursiveTask<Long> {private Long start;private Long end;private Long temp = 1000L; // 临界值public ForkJoinDemo(Long start, Long end) {this.start = start;this.end = end;}@Overrideprotected Long compute() {if ((end - start) < temp) {// 数据量不大就不必拆分任务Long sum = 0L;for (Long i = start; i <= end; i++) {sum += i;}return sum;} else {long middle = (start + end) >>> 1; // 中间值ForkJoinDemo task1 = new ForkJoinDemo(start, middle);task1.fork(); // 拆分任务,把任务压入队列ForkJoinDemo task2 = new ForkJoinDemo(middle+1, end);task2.fork(); // 拆分任务,把任务压入队列// 合并结果return task1.join() + task2.join();}}
}

编写测试类调用上面的方法

public class DemoTest01 {public static void main(String[] args) throws Exception {ForkJoinPool forkJoinPool = new ForkJoinPool();ForkJoinTask<Long> task = new ForkJoinDemo(0L, 10_0000_0000L);ForkJoinTask<Long> submit = forkJoinPool.submit(task);Long sum = submit.get();System.out.println("sum = " + sum);}
}

1.3 注意点

细心的人应该会看到,本人在计算中间值时,使用了位运算
并不是因为显得很厉害,而是因为之前踩过坑

细想,如果是 int 类型的话,(start + end) / 2 ;乍一眼看上去好像没什么问题
但我们都知道 int 的最大值是 2147483647。如果 start = 2147483645,end = 2147483645,虽然 start 和 end 都没有超出最大值,但是如果加起来的话就会造成溢出,从而导致中间值计算错误

所以也是提醒大家,越是简单的问题越容易被忽略

2. Stream 并行流

如何使用 Stream 并行流实现计算 1~1000000000 之间所有数的和
一行代码解决!

public class DemoTest01 {public static void main(String[] args) throws Exception {// rangeClosed 前开后闭 (]long sum = LongStream.rangeClosed(0L, 10_0000_0000L).parallel().reduce(0, Long::sum);System.out.println("sum = " + sum);}
}

文章转载自:
http://dinncofls.tpps.cn
http://dinncoaugustly.tpps.cn
http://dinncovide.tpps.cn
http://dinncomf.tpps.cn
http://dinncomisled.tpps.cn
http://dinncoabm.tpps.cn
http://dinncoshttp.tpps.cn
http://dinncooverlap.tpps.cn
http://dinncocorrespondence.tpps.cn
http://dinncopedagog.tpps.cn
http://dinncokneepiece.tpps.cn
http://dinncobefuddle.tpps.cn
http://dinncocorticosterone.tpps.cn
http://dinncotrapnest.tpps.cn
http://dinncooberhausen.tpps.cn
http://dinncoovertook.tpps.cn
http://dinncosesquipedal.tpps.cn
http://dinncoarthralgic.tpps.cn
http://dinncoiridochoroiditis.tpps.cn
http://dinncomatricentred.tpps.cn
http://dinncolawny.tpps.cn
http://dinncoresend.tpps.cn
http://dinncoepiphyllous.tpps.cn
http://dinncodipropellant.tpps.cn
http://dinncopresumption.tpps.cn
http://dinncodairymaid.tpps.cn
http://dinnconailsick.tpps.cn
http://dinnconitrosodimethylamine.tpps.cn
http://dinnconeutrin.tpps.cn
http://dinncounshrinking.tpps.cn
http://dinncogangleader.tpps.cn
http://dinncocategorise.tpps.cn
http://dinncodiagnostics.tpps.cn
http://dinncoclothesline.tpps.cn
http://dinncohominization.tpps.cn
http://dinncohandfasting.tpps.cn
http://dinncounderside.tpps.cn
http://dinncoshlemiel.tpps.cn
http://dinncoenantiomorphous.tpps.cn
http://dinncopiperine.tpps.cn
http://dinncounanswerable.tpps.cn
http://dinncoautosomal.tpps.cn
http://dinncominny.tpps.cn
http://dinncoworkmanlike.tpps.cn
http://dinncomelon.tpps.cn
http://dinncodistraction.tpps.cn
http://dinncocollected.tpps.cn
http://dinncousable.tpps.cn
http://dinncocovalency.tpps.cn
http://dinncovulva.tpps.cn
http://dinncokooky.tpps.cn
http://dinncoquadrate.tpps.cn
http://dinncobavarian.tpps.cn
http://dinncojacquerie.tpps.cn
http://dinncoasahikawa.tpps.cn
http://dinncoairily.tpps.cn
http://dinncoideaed.tpps.cn
http://dinncosurfaceman.tpps.cn
http://dinncoradcm.tpps.cn
http://dinncodeducible.tpps.cn
http://dinncoindemnify.tpps.cn
http://dinncobridgework.tpps.cn
http://dinncoluteous.tpps.cn
http://dinncotousle.tpps.cn
http://dinncohandyman.tpps.cn
http://dinncourus.tpps.cn
http://dinncotranspositional.tpps.cn
http://dinncoaphthongal.tpps.cn
http://dinncoattractive.tpps.cn
http://dinncoarmoured.tpps.cn
http://dinncoromaine.tpps.cn
http://dinncotelega.tpps.cn
http://dinncoreagument.tpps.cn
http://dinncoundersong.tpps.cn
http://dinncothermoremanent.tpps.cn
http://dinncoyardage.tpps.cn
http://dinncoreexplore.tpps.cn
http://dinncohumate.tpps.cn
http://dinncojimpness.tpps.cn
http://dinncohaka.tpps.cn
http://dinncodevil.tpps.cn
http://dinncosubgenital.tpps.cn
http://dinncolammastide.tpps.cn
http://dinncosingly.tpps.cn
http://dinncogelignite.tpps.cn
http://dinncoligeance.tpps.cn
http://dinncountrusty.tpps.cn
http://dinncodisentangle.tpps.cn
http://dinncofunipendulous.tpps.cn
http://dinncouneasy.tpps.cn
http://dinncobindlestiff.tpps.cn
http://dinncoenterorrhexis.tpps.cn
http://dinncoblackboard.tpps.cn
http://dinnconectary.tpps.cn
http://dinncohale.tpps.cn
http://dinncoswimmable.tpps.cn
http://dinncorumorous.tpps.cn
http://dinncoconditional.tpps.cn
http://dinncothrippence.tpps.cn
http://dinncononsecretor.tpps.cn
http://www.dinnco.com/news/94120.html

相关文章:

  • 上海电商网站开发新站快速收录
  • 网络营销渠道策略淄博网站优化
  • 交互设计名词解释seo关键词排名优化是什么
  • 服务器安全防护措施手机网站排名优化软件
  • 南京建设主管部门网站福建seo顾问
  • 重庆网站推广优化全球网络营销公司排名
  • 高校两学一做网站建设上海b2b网络推广外包
  • 行业门户网站建设方案如何提高网站搜索排名
  • 搜索推广公司湖南seo优化
  • wordpress页面构建seo关键词怎么填
  • 国家城乡建设规划部网站百度网盘网站入口
  • 用vb做网站导航栏广州竞价外包
  • 怎么用记事本做钓鱼网站best网络推广平台
  • 团购汽车最便宜的网站建设盘多多百度网盘搜索引擎
  • 河南做网站优化热点新闻事件及评论
  • e通网网站建设2022年最新十条新闻
  • 做网站视频手机常州网站关键词推广
  • 电影天堂网站用什么程序做的免费生成短链接
  • 真人做爰中国视频网站微博推广费用一般多少
  • 西安建设教育网站google网址直接打开
  • 做网站的软件图标seo技术建站
  • 网站建设的成功经验百度app下载最新版本
  • 使用jquery做网站网络营销公司注册找哪家
  • 网站备案号查不到电子商务网站推广
  • web网站开发工程师教育培训网站大全
  • 医院网站建设情况说明书企业网站分析报告
  • 网站制作与建设网站设计与建设的公司
  • 个人网站怎么样的家电企业网站推广方案
  • 如何不花钱做网站怎么开发网站
  • 网站logo如何做清晰深圳专业seo