简体   繁体   English

多个Java线程并控制跨多个内核的线程分配

[英]Multiple Java threads and control over thread distribution across muliple cores

When we have multiple core machines and with Java concurrency, multiple threads across multiple cores is possible. 当我们有多台核心机器并具有Java并发性时,跨多个核心的多个线程是可能的。 Also, we do have stream in Java which can help distribute the work. 另外,我们确实有Java流,可以帮助分发工作。

However, how do we ensure that threads are properly distributed across the cores so that we make efficient use of the cores? 但是,我们如何确保线程在内核之间正确分配,以便我们有效利用内核?

How does the thread distribution differ across windows and Linux operating systems? Windows和Linux操作系统之间的线程分布有何不同? And how does it differ across Intel and AMD processors? 在英特尔和AMD处理器之间有何不同? Do we need to handle threads in specific ways for different OS and processors? 我们需要针对不同的操作系统和处理器以特定方式处理线程吗?

Java Thread uses the native thread facilities of the underlying OS. Java Thread使用基础OS的本机线程功能。 So, you have no control, within Java, over how the OS runs the Thread, apart from whatever values the Thread class allows you to pass to the OS. 因此,在Java中,除了Thread类允许您传递给OS的任何值外,您都无法控制OS如何运行Thread。 How Java and the OS handle those values depends on the OS and the Java implementation. Java和OS如何处理这些值取决于OS和Java实现。

I don't get the impression you are an advanced systems programmer, so I suggest you let Java handle efficiency. 我不觉得您是高级系统程序员,所以建议您让Java处理效率。

Typically you would want to match the number of computational threads to the number of hardware threads. 通常,您需要将计算线程数与硬件线程数进行匹配。

This is the default behaviour of ForkJoinPool , configurable through other constructor or, for the common pool, the java.util.concurrent.ForkJoinPool.common.parallelism system property. 这是ForkJoinPool的默认行为,可以通过其他构造函数进行配置,或者对于公共池,可以通过java.util.concurrent.ForkJoinPool.common.parallelism系统属性进行配置。 You may want to lower this if there is other computational intensive work happening on the system. 如果系统上正在进行其他计算密集型工作,则可能需要降低此值。 The Stream API doesn't appear to specify behaviour (I may be wrong), but reasonable implementations would use the current ForkJoinPool (ie the pool of the current task is running in, or the common pool if running outside of any pool). Stream API似乎没有指定行为(我可能是错的),但是合理的实现将使用当前的ForkJoinPool (即当前任务的池正在运行,或者如果在任何池之外运行,则为公共池)。

As ever, parallelism may make your program speed go down as well as up. 与以往一样,并行性可能会使您的程序速度变慢。

Scheduling threads is down to the platform, typically delegated to the operating system. 调度线程取决于平台,通常委托给操作系统。 Some JRE implementations in the past have moved Java threads between OS threads, but that's about handling I/O blocking. 过去,某些JRE实现已将Java线程移至OS线程之间,但这与处理I / O阻塞有关。

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

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