简体   繁体   English

在具有上下文切换的线程池执行器中执行连续运行的线程/任务

[英]Executing continuously running threads/task in thread pool executor with context switching

I want to run n tasks continuously, however those tasks are memory intensive I want only x of them to be active at a time.我想连续运行n 个任务,但是这些任务是 memory 密集型我希望一次只有x个处于活动状态。 But ultimately all those n tasks should run by context switching between them.但最终所有这n 个任务都应该通过它们之间的上下文切换来运行。

In short, I want another implementation of FixedThreadPool , where extra tasks should also run with context switching.简而言之,我想要另一个FixedThreadPool实现,其中额外的任务也应该通过上下文切换运行。

Do we have Thread Pool variant which achieves same?我们是否有实现相同的线程池变体? Or any other way to implement it?或者任何其他方式来实现它?

UPDATE: After reading a bit and reading answer below, decided to "divide and conquer" ie breaking continuously running task into units of small short lived tasks and submitting those again and again to FixedThreadPool更新:在阅读了一些并阅读下面的答案后,决定“分而治之”,即将连续运行的任务分解为小短命任务的单元,然后一次又一次地提交给FixedThreadPool

One could write a paper on the subject but let us keep it simple and concise.人们可以写一篇关于这个主题的论文,但让我们保持简单明了。

In short, I want another implementation of FixedThreadPoolSize, where extra tasks should also run with context switching.简而言之,我想要另一个 FixedThreadPoolSize 的实现,其中额外的任务也应该通过上下文切换运行。

To achieve that, one would need a Thread Pool that allows to explicitly perform affinity between a Thread and a core.为了实现这一点,需要一个允许显式执行线程和核心之间的关联的线程池。 And (as far I know) such a Thread Pool is not officially provided with Java.而且(据我所知)Java 并没有正式提供这样的线程池。 Which makes sense, since one of the goals of abstractions such as Thread Pools (in Java) is to increase the level of abstraction, even to the point of abstracting concepts such as a thread ( ie, Executor).这是有道理的,因为诸如线程池(在 Java 中)之类的抽象目标之一是提高抽象级别,甚至达到抽象概念(例如线程(Executor))的程度。 Therefore, it does not come as a surprise that such a low-level feature (as mapping threads to cores) is not provided out-of-the-box.因此,这种低级功能(如将线程映射到内核)不是开箱即用的就不足为奇了。

Do we have Thread Pool variant which achieves same?我们是否有实现相同的线程池变体? Or any other way to implement it?或者任何其他方式来实现它?

Unless you are running your code in a Non-Uniform Memory Access (NUMA) architecture, I don't see the benefits of such a low level feature in the context of your program.除非您在Non-Uniform Memory Access (NUMA) 架构中运行代码,否则我看不到在您的程序上下文中这种低级功能的好处。

My use case is I have to run n tasks continuously.我的用例是我必须连续运行 n 个任务。 But as those tasks are memory intensive I want only x of them to be active at a time.但由于这些任务是 memory 密集型任务,我希望一次只有 x 个处于活动状态。 But ultimately all those n tasks should run by context switching between them.但最终所有这 n 个任务都应该通过它们之间的上下文切换来运行。

If you run n tasks and n Threads, and the hardware where the code is running has c cores, where n >> c , then inevitably the SO will map multiple threads to the same core.如果您运行n任务和n线程,并且运行代码的硬件具有c内核,其中n >> c ,那么 SO 不可避免地会将 Z1D78DC8ED51214E518B5114FE24 多个线程。 Hence, you will have your context switching for free .因此,您将免费进行上下文切换

Finally, before actually opting to run more Threads than cores, profile your code, accordingly.最后,在实际选择运行比内核更多的线程之前,相应地分析您的代码。 For instance, running the code with the same number threads as cores, then with doubling the number of threads until it stops scaling.例如,使用与内核相同数量的线程运行代码,然后将线程数量增加一倍,直到它停止扩展。 It might so happen that your code does even scale with more threads than cores.您的代码甚至可能使用比内核更多的线程进行扩展。

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

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