简体   繁体   English

Executors.newFixedThreadPool()-此操作的成本是多少

[英]Executors.newFixedThreadPool() - how expensive is this operation

I have a requirement where i need to process some tasks for current live shows . 我有一个需要处理当前现场表演的任务的要求。 This is a scheduled tasks and runs every minute. 这是计划的任务,每分钟运行一次。

At any given minute, there can be any number of live shows (though number cannot be that large, approx max 10 ). 在任何给定的分钟,可以有任意数量的现场表演 (尽管数量不能那么大, 最大不能超过10 )。 There are more than 20 functionalities needs to be done for all the live shows. 所有现场表演都需要完成20多种功能。 or say 20 worker classes are there , all are doing there job . 或说有20个工人阶级,都在那里做工作

Let say for first functionality, there are 5 shows, then after few minutes shows reduced to 2, then again after few minutes shows increase to 7 . 假设对于第一个功能,有5个显示,然后在几分钟后显示减少到2个,然后在几分钟后再次显示增加到7个

Currently i am doing something like this, 目前我正在做这样的事情,

int totalShowsCount = getCurrentShowsCount();
ExecutorService executor = Executors.newFixedThreadPool(showIds.size());

The above statements gets executed every minute. 上面的语句每分钟执行一次。

Problem Statement 问题陈述

1.) How much expensive the above operation be..??. 1.)上述操作多少钱。 Creating fixedThreadPool at every given minute. 每隔一分钟创建一次fixedThreadPool。

2.) What can i do to optimize my solution, should i use a fixed thread pool, say (10), and maybe 3 or 5 or 6 or any number of threads getting utilized at any given minute. 2.)我应该做些什么来优化我的解决方案,如果我使用固定的线程池,例如(10),也许是3或5或6,或者在任何给定的分钟内使用了任意数量的线程。

Can i create a fixed thread pool at worker level, and maintain it and utilize that. 我可以在工作人员级别上创建固定线程池,并对其进行维护和利用。

FYI, using Java8, if any better approach is available. 仅供参考,如果有更好的方法,请使用Java8。

How much expensive the above operation be..??. 上述操作多少钱.. ?? Creating fixedThreadPool at every given minute. 每隔一分钟创建一次fixedThreadPool。

Creating a thread pool is a relatively expensive operation which can take milli-seconds. 创建线程池是一个相对昂贵的操作,可能需要数毫秒。 You don't want to be doing this many times per second. 您不想每秒执行多次。

A second is an eternity for a computer, if you have a 36 core machine it can execute as much as 100 billion instructions in that amount of time. 一秒钟是计算机的永恒,如果您拥有36核的计算机,那么在那段时间内它可以执行多达1000亿条指令。 A minute is a very, very long time, and if you only do something once a minute you could even restart your JVM every minute and still get reasonable throughput. 一分钟是非常非常长的时间,如果您每分钟只执行一次操作,则您甚至可以每分钟重新启动JVM,并且仍然可以获得合理的吞吐量。

What can i do to optimize my solution, should i use a fixed thread pool, say (10), and maybe 3 or 5 or 6 or any number of threads getting utilized at any given minute. 我应该做些什么来优化我的解决方案,如果我使用固定的线程池,例如(10),也许是3或5或6,或者在任何给定的分钟内使用了任意数量的线程。

Possibly, it depends on what you are doing. 可能取决于您在做什么。 Without most analysis you could say for sure. 没有大多数分析,您可以肯定地说。 Note: If you are using parallelStream(), if not you should see if you can, you can use the built in ForkJoinPool.commonPool() and not need to create another pool. 注意:如果使用的是parallelStream(),如果没有,则应查看是否可以,可以使用内置的ForkJoinPool.commonPool() ,而无需创建其他池。 But again, this depends on what you are doing. 但这又取决于您在做什么。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM