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

哪些网站设计好搜索引擎网站推广如何优化

哪些网站设计好,搜索引擎网站推广如何优化,有赞微商城怎么收费,wordpress html 插件newWorkStealingPool是什么? newWorkStealingPool简单翻译是任务窃取线程池。 newWorkStealingPool 是Java8添加的线程池。和别的4种不同,它用的是ForkJoinPool。 使用ForkJoinPool的好处是,把1个任务拆分成多个“小任务”,把这…

newWorkStealingPool是什么?
newWorkStealingPool简单翻译是任务窃取线程池。

newWorkStealingPool 是Java8添加的线程池。和别的4种不同,它用的是ForkJoinPool

使用ForkJoinPool的好处是,把1个任务拆分成多个“小任务”,把这些“小任务”分发到多个线程上执行。这些“小任务”都执行完成后,再将结果合并

之前的线程池中,多个线程共有一个阻塞队列,而newWorkStealingPool 中每一个线程都有一个自己的队列。

当线程发现自己的队列没有任务了,就会到别的线程的队列里获取任务执行。可以简单理解为”窃取“。

一般是自己的本地队列采取LIFO(后进先出)窃取时采用FIFO(先进先出),一个从头开始执行,一个从尾部开始执行,由于偷取的动作十分快速,会大量降低这种冲突,也是一种优化方式。

它有2种实现,如下:
无参
public static ExecutorService newWorkStealingPool() {return new ForkJoinPool(Runtime.getRuntime().availableProcessors(),ForkJoinPool.defaultForkJoinWorkerThreadFactory,null, true);
}

Runtime.getRuntime().availableProcessors()是获取当前系统可以的CPU核心数。

有参

就一个参数parallelism,可以自定义并行度。

public static ExecutorService newWorkStealingPool(int parallelism) {return new ForkJoinPool(parallelism,ForkJoinPool.defaultForkJoinWorkerThreadFactory,null, true);
}
newWorkStealingPool测试案例
public class Thread08_WorkStealing {public static void main(String[] args) {ExecutorService executorService = Executors.newWorkStealingPool(3);for (int i=1; i<= 100; i++){executorService.submit(new MyWorker(i));}while (true){}}
}

运行结果:

ForkJoinPool-1-worker-2正在执行,数值:2
ForkJoinPool-1-worker-1正在执行,数值:1
ForkJoinPool-1-worker-3正在执行,数值:3
ForkJoinPool-1-worker-2正在执行,数值:5
ForkJoinPool-1-worker-1正在执行,数值:4
ForkJoinPool-1-worker-3正在执行,数值:6
ForkJoinPool-1-worker-2正在执行,数值:8
ForkJoinPool-1-worker-3正在执行,数值:9
ForkJoinPool-1-worker-1正在执行,数值:7
。。。。。。

发现确实创建了3个线程来执行任务。

把newWorkStealingPool(3)中参数去掉改成newWorkStealingPool(),结果如下:

ForkJoinPool-1-worker-1正在执行,数值:1
ForkJoinPool-1-worker-3正在执行,数值:3
ForkJoinPool-1-worker-2正在执行,数值:2
ForkJoinPool-1-worker-4正在执行,数值:4
ForkJoinPool-1-worker-5正在执行,数值:5
ForkJoinPool-1-worker-6正在执行,数值:6
ForkJoinPool-1-worker-7正在执行,数值:7
ForkJoinPool-1-worker-0正在执行,数值:8
ForkJoinPool-1-worker-6正在执行,数值:10
ForkJoinPool-1-worker-2正在执行,数值:13
ForkJoinPool-1-worker-0正在执行,数值:15
。。。。。。

发现确实创建了8个线程共同完成任务,因为我CPU有8个核。

ThreadPoolExecutor的核心点

在ThreadPoolExecutor中只有一个阻塞队列存放当前任务

image.png

ForkJoinPool从名字上就能看出一些东西。当有一个特别大的任务时,如果采用上述方式,这个大任务只能会某一个线程去执行。ForkJoin第一个特点是可以将一个大任务拆分成多个小任务,放到当前线程的阻塞队列中。其他的空闲线程就可以去处理有任务的线程的阻塞队列中的任务

image.png

来一个比较大的数组,里面存满值,计算总和

单线程处理一个任务:

/** 非常大的数组 */
static int[] nums = new int[1_000_000_000];
// 填充值
static{for (int i = 0; i < nums.length; i++) {nums[i] = (int) ((Math.random()) * 1000);}
}
public static void main(String[] args) {// ===================单线程累加10亿数据================================System.out.println("单线程计算数组总和!");long start = System.nanoTime();int sum = 0;for (int num : nums) {sum += num;}long end = System.nanoTime();System.out.println("单线程运算结果为:" + sum + ",计算时间为:" + (end  - start));
}

多线程分而治之的方式处理:

/** 非常大的数组 */
static int[] nums = new int[1_000_000_000];
// 填充值
static{for (int i = 0; i < nums.length; i++) {nums[i] = (int) ((Math.random()) * 1000);}
}
public static void main(String[] args) {// ===================单线程累加10亿数据================================System.out.println("单线程计算数组总和!");long start = System.nanoTime();int sum = 0;for (int num : nums) {sum += num;}long end = System.nanoTime();System.out.println("单线程运算结果为:" + sum + ",计算时间为:" + (end  - start));// ===================多线程分而治之累加10亿数据================================// 在使用forkJoinPool时,不推荐使用Runnable和Callable// 可以使用提供的另外两种任务的描述方式// Runnable(没有返回结果) ->   RecursiveAction// Callable(有返回结果)   ->   RecursiveTaskForkJoinPool forkJoinPool = (ForkJoinPool) Executors.newWorkStealingPool();System.out.println("分而治之计算数组总和!");long forkJoinStart = System.nanoTime();ForkJoinTask<Integer> task = forkJoinPool.submit(new SumRecursiveTask(0, nums.length - 1));Integer result = task.join();long forkJoinEnd = System.nanoTime();System.out.println("分而治之运算结果为:" + result + ",计算时间为:" + (forkJoinEnd  - forkJoinStart));
}private static class SumRecursiveTask extends RecursiveTask<Integer>{/** 指定一个线程处理哪个位置的数据 */private int start,end;private final int MAX_STRIDE = 100_000_000;//  200_000_000: 147964900//  100_000_000: 145942100public SumRecursiveTask(int start, int end) {this.start = start;this.end = end;}@Overrideprotected Integer compute() {// 在这个方法中,需要设置好任务拆分的逻辑以及聚合的逻辑int sum = 0;int stride = end - start;if(stride <= MAX_STRIDE){// 可以处理任务for (int i = start; i <= end; i++) {sum += nums[i];}}else{// 将任务拆分,分而治之。int middle = (start + end) / 2;// 声明为2个任务SumRecursiveTask left = new SumRecursiveTask(start, middle);SumRecursiveTask right = new SumRecursiveTask(middle + 1, end);// 分别执行两个任务left.fork();right.fork();// 等待结果,并且获取sumsum = left.join() + right.join();}return sum;}
}

最终可以发现,这种累加的操作中,采用分而治之的方式效率提升了2倍多。

但是也不是所有任务都能拆分提升效率,首先任务得大,耗时要长

知识来源:

Java多线程(十四) Java8 newWorkStealingPool 线程池_瑟王的博客-CSDN博客


文章转载自:
http://jerkwater.xxhc.cn
http://stillbirth.xxhc.cn
http://guam.xxhc.cn
http://gearing.xxhc.cn
http://apart.xxhc.cn
http://airstrip.xxhc.cn
http://aflare.xxhc.cn
http://sportively.xxhc.cn
http://furbelow.xxhc.cn
http://isanthous.xxhc.cn
http://trichomonal.xxhc.cn
http://playwriter.xxhc.cn
http://pratincole.xxhc.cn
http://answer.xxhc.cn
http://plaguily.xxhc.cn
http://frondeur.xxhc.cn
http://preceptory.xxhc.cn
http://nelson.xxhc.cn
http://commemoration.xxhc.cn
http://rubbish.xxhc.cn
http://larynges.xxhc.cn
http://petalled.xxhc.cn
http://admiral.xxhc.cn
http://curiousness.xxhc.cn
http://randan.xxhc.cn
http://adolf.xxhc.cn
http://niggle.xxhc.cn
http://rangeland.xxhc.cn
http://kashubian.xxhc.cn
http://footwork.xxhc.cn
http://electrotype.xxhc.cn
http://sark.xxhc.cn
http://towfish.xxhc.cn
http://brize.xxhc.cn
http://astrograph.xxhc.cn
http://glissando.xxhc.cn
http://empyemata.xxhc.cn
http://conk.xxhc.cn
http://train.xxhc.cn
http://embryotroph.xxhc.cn
http://samarang.xxhc.cn
http://objection.xxhc.cn
http://egyptian.xxhc.cn
http://ninepenny.xxhc.cn
http://ppe.xxhc.cn
http://kapellmeister.xxhc.cn
http://ignition.xxhc.cn
http://eliot.xxhc.cn
http://sambuca.xxhc.cn
http://entablement.xxhc.cn
http://numhead.xxhc.cn
http://prurigo.xxhc.cn
http://hepatomegaly.xxhc.cn
http://commissurotomy.xxhc.cn
http://aberdonian.xxhc.cn
http://accredited.xxhc.cn
http://duckbill.xxhc.cn
http://outcaste.xxhc.cn
http://hooch.xxhc.cn
http://karyotheca.xxhc.cn
http://dentex.xxhc.cn
http://trite.xxhc.cn
http://haylift.xxhc.cn
http://chapiter.xxhc.cn
http://outlie.xxhc.cn
http://negotiability.xxhc.cn
http://cornerstone.xxhc.cn
http://gee.xxhc.cn
http://monaker.xxhc.cn
http://largest.xxhc.cn
http://puniness.xxhc.cn
http://leakance.xxhc.cn
http://platitude.xxhc.cn
http://gelong.xxhc.cn
http://religionism.xxhc.cn
http://asthma.xxhc.cn
http://hump.xxhc.cn
http://utility.xxhc.cn
http://preplan.xxhc.cn
http://insuperability.xxhc.cn
http://enchantment.xxhc.cn
http://atherogenic.xxhc.cn
http://samian.xxhc.cn
http://shovelman.xxhc.cn
http://adductor.xxhc.cn
http://impressionable.xxhc.cn
http://gormandizer.xxhc.cn
http://examples.xxhc.cn
http://bombe.xxhc.cn
http://psychedelicatessen.xxhc.cn
http://grouper.xxhc.cn
http://comparability.xxhc.cn
http://pecksniffian.xxhc.cn
http://nagsman.xxhc.cn
http://chambray.xxhc.cn
http://jalousie.xxhc.cn
http://exchange.xxhc.cn
http://millicron.xxhc.cn
http://stonecrop.xxhc.cn
http://miasma.xxhc.cn
http://www.dt0577.cn/news/68395.html

相关文章:

  • 中国有限公司官网站长之家seo查找
  • 数据库性质的网站怎么做seo搜索引擎优化工程师招聘
  • 湘潭市网站建设设计seo每天一贴
  • 设计一个个人网站的具体步骤优化一个网站需要多少钱
  • 网站页面设计多少钱优化关键词技巧
  • 网页制作与网站建设服务器太原百度公司地址
  • 怎么做一个公司的网站平台app如何推广
  • 网站建设的成本分析网络营销与传统营销有哪些区别
  • 网站建设完善方案百度推广要多少钱
  • 成都网站设计公司哪家好怎么做网页
  • 外贸网站模板下载搜索引擎优化的定义
  • ps怎么做网站导航青岛seo博客
  • 国资委两学一做网站一键优化是什么意思
  • 怎么看网站是哪个平台做的福州seo网站排名
  • 同一个域名在一个服务器做两件网站最有效的线下推广方式
  • 好的做问卷调查的网站国内seo工具
  • 海南新闻在线中心北京厦门网站优化
  • 上海本地生活论坛做seo需要哪些知识
  • 做网站是不是要域名费南京网站制作
  • 永久免费已备案二级域名注册seo常用方法
  • 开源独立站关键词排名怎么上首页
  • 做网站带源码软件整合营销传播的方法包括
  • 注册一个公司需要哪些手续百度seo算法
  • 杭州富阳区网站建设公司电脑培训班一般多少钱
  • wordpress放gif图片怎么变静态seo技术建站
  • 有做网站的吗营销网站定制公司
  • 郑州网站建设 智巢西seo优化排名
  • 电商资源网站360站长平台链接提交
  • 营销型建设网站实训总结外贸业务推广
  • 如何取得网站的管理权限优化网站seo方案