简体   繁体   English

如何强制两个Java线程在同一处理器/核心上运行?

[英]How to force two Java threads to run on same processor/core?

I would like a solution that doesn't include critical sections or similar synchronization alternatives. 我想要一个不包含关键部分或类似同步替代方案的解决方案。 I'm looking for something similar the equivalent of Fiber (user level threads) from Windows. 我正在寻找类似于Windows的光纤(用户级线程)的东西。

The OS manages what threads are processed on what core. 操作系统管理在哪个核心上处理哪些线程。 You will need to assign the threads to a single core in the OS. 您需要将线程分配给OS中的单个核心。

For instance. 例如。 On windows, open task manager, go to the processes tab and right click on the java processes... then assign them to a specific core. 在Windows上,打开任务管理器,转到进程选项卡并右键单击java进程...然后将它们分配给特定的核心。

That is the best you are going to get. 这是你将获得的最好的。

To my knowledge there is no way you can achieve that. 据我所知,你无法实现这一目标。
Simply because the OS manages running threads and distributes resources according to it's scheduler. 仅仅因为操作系统管理正在运行的线程并根据它的调度程序分配资源。

Edit: 编辑:
Since your goal is to have a "spare" core to run other processes on I'd suggest you use a thread manager and get the number of cores on the system (x) and then spawn at most x-1 threads on the specific system. 由于你的目标是有一个“备用”核心来运行其他进程,我建议你使用一个线程管理器并获得系统上的核心数量(x),然后在特定系统上产生大多数x-1个线程。 That way you'll have your spare core. 这样你就可以获得备用核心。

The former statements still apply, you cannot specify which cores to run threads on unless you in the OS specify it. 前面的语句仍然适用,除非您在操作系统中指定,否则您无法指定运行线程的核心。 But from java, no. 但是从java,没有。

Short of assigning the entire JVM to a single core, I'm not sure how you'd be able to do this. 如果没有将整个JVM分配给单个核心,我不确定你是如何做到这一点的。 In Linux, you can use taskset: 在Linux中,您可以使用taskset:

http://www.cyberciti.biz/tips/setting-processor-affinity-certain-task-or-process.html http://www.cyberciti.biz/tips/setting-processor-affinity-certain-task-or-process.html

I suppose you could run your JVM within a virtualized environment (eg, VirtualBox/VMWare instance) with one processor allocated, but I'm not sure that that gets you what you want. 我想你可以在虚拟化环境(例如,VirtualBox / VMWare实例)中运行你的JVM并分配一个处理器,但我不确定那能得到你想要的东西。

I read this as asking if a Java application can control the thread affinity itself. 我读这是为了询问Java应用程序是否可以控制线程亲和性本身。 Java does not provide any way to control this. Java没有提供任何方法来控制它。 It is treated as the business of the host operating system. 它被视为主机操作系统的业务。

If anything can do it, the OS can, and they typically can, though the tools you use for thread pinning will be OS specific. 如果有什么可以做的话,操作系统可以,而且他们通常可以,尽管您用于线程固定的工具将是特定于操作系统的。 (But if the OS is itself is virtualized, there are two levels of pinning. I don't know if that is going to work / be practical.) (但如果操作系统本身是虚拟化的,那么有两个级别的固定。我不知道这是否会起作用/实用。)


There don't appear to be any relevant Hotspot JVM thread tuning options in modern JVMs. 现代JVM中似乎没有任何相关的Hotspot JVM线程调整选项。

If you were using a Rockit JVM you could choose between "native threads" (where there is a 1-1 mapping between Java and OS threads) and "thin threads" where multiple Java threads are multiplexed onto a small number of OS threads. 如果您使用的是Rockit JVM,您可以在“本机线程”(Java和OS线程之间存在1-1映射)和“细线程”之间进行选择,其中多个Java线程被复用到少量OS线程上。 But AFAIK, JRocket "thin threads" are only supported in 32bit mode, and they don't allow you to tune the number of OS threads used. 但AFAIK,JRocket“细线程”仅支持32位模式,并且它们不允许您调整所使用的OS线程数。

This is really the kind of question that you should be asking under a Sun support contract. 这实际上是你应该根据Sun支持合同提出的问题。 They have people who have spent years figuring out how to get the best performance out of big Java apps. 他们有多年来一直在想办法如何从大型Java应用程序中获得最佳性能。

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

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