如何设置 Java 线程的 CPU 核心关联?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13392379/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to set a Java thread's cpu core affinity?
提问by Sachin
I searched previous postings on the similar topic but could not find a befitting answer therefore asking this question. Your help in answering it is highly appreciated.
我搜索了以前关于类似主题的帖子,但找不到合适的答案,因此提出了这个问题。非常感谢您帮助回答。
I am aware of setting a process's affinity to a particular CPU core by taskset command in Linux. But I want to set a Java thread's affinity to a particular cpu core so that other threads belonging to the same process can run on all remaining cores. For example if I have a process containing 10 threads with 4-core machine, I would like to reserve core-1 for a thread and let remaining 9 threads run on remaining 3-cores. Can it be done and how?
我知道在 Linux 中通过 taskset 命令设置进程与特定 CPU 内核的关联。但我想设置一个 Java 线程对特定 cpu 核心的关联,以便属于同一进程的其他线程可以在所有剩余的核心上运行。例如,如果我有一个包含 10 个线程和 4 核机器的进程,我想为一个线程保留 core-1,让剩余的 9 个线程在剩余的 3 核上运行。可以做到吗?如何做到?
Thanks Sachin
谢谢萨钦
采纳答案by Alex T
Say 2241 is the pid of your java process. Run:
说 2241 是你的java进程的pid。跑:
jstack 2241
This gives you a list of threads. Find yours there and note the nid field. Say nid=0x8e9, which converts to base 10 as 2281. Then run:
这为您提供了一个线程列表。在那里找到你的并记下 nid 字段。假设 nid=0x8e9,它将以 10 为基数转换为 2281。然后运行:
taskset -p -c 0 2281
Done.
完毕。
回答by Timr
Unfortunately, you cannot assign Java Threads to a specific core. What you can do, however, is set Thread Prioritiesto prioritize the threads (assuming that this would accomplish the same thing)
不幸的是,您不能将 Java 线程分配给特定的内核。但是,您可以做的是设置线程优先级以优先考虑线程(假设这将完成相同的事情)
Alternatively, you could use JNI, but that would be completely overkill.
或者,您可以使用 JNI,但这完全是矫枉过正。
回答by ramsinb
Remember the Java application that your running is actually running in a JVM which is in turn running on the OS. For you to be able to directly interact with the CPU you'd need a low level programming language (e.g. C).
请记住,您运行的 Java 应用程序实际上是在 JVM 中运行的,而 JVM 又在操作系统上运行。为了能够直接与 CPU 交互,您需要一种低级编程语言(例如 C)。
As suggested in another answer you can use JNI to interact with lower level language (like C) to do what you want however you'd have to delegate concurrency (threads managed within that lower level langaguge) to it...
正如另一个答案中所建议的那样,您可以使用 JNI 与较低级别的语言(如 C)进行交互以执行您想要的操作,但是您必须将并发(在该较低级别的语言中管理的线程)委托给它......
回答by rdalmeida
You can do that in plain Java using JNA. There is no need to use taskset. Just remember that thread affinity is pointless unless you have previously isolated the corefrom kernel/user threads and hardware interrupts. I am affiliated with Coral Blocks which has developed CoralThreadswhich does exactly that.
您可以使用 JNA 在普通 Java 中做到这一点。无需使用任务集。请记住,除非您之前已将内核与内核/用户线程和硬件中断隔离,否则线程关联是没有意义的。我隶属于 Coral Blocks,它开发了CoralThreads,它正是这样做的。