Java 线程亲和性

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2238272/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 05:06:33  来源:igfitidea点击:

Java thread affinity

javamultithreadingschedulingsetthreadaffinitymask

提问by Dave

Does anybody know of a way to lock down individual threads within a Java process to specific CPU cores (on Linux)? I've done this in C, but can't find how to do this in Java. My instincts are that this will require a JNI call, but I was hoping someone here might have some insight or might have done it before.

有人知道将 Java 进程中的单个线程锁定到特定 CPU 内核的方法吗(在 Linux 上)?我已经用 C 完成了这个,但找不到如何在 Java 中做到这一点。我的直觉是这将需要一个 JNI 调用,但我希望这里的某个人可能有一些见解或可能以前做过。

Thanks!

谢谢!

采纳答案by BegemoT

You can't do this in pure java. But if you really need it -- you can use JNI to call native code which do the job. This is the place to start with:

你不能在纯 Java 中做到这一点。但是如果你真的需要它——你可以使用 JNI 来调用完成这项工作的本机代码。这是开始的地方:

http://ovatman.blogspot.com/2010/02/using-java-jni-to-set-thread-affinity.html

http://ovatman.blogspot.com/2010/02/using-java-jni-to-set-thread-affinity.html

http://blog.toadhead.net/index.php/2011/01/22/cputhread-affinity-in-java/

http://blog.toadhead.net/index.php/2011/01/22/cputhread-affinity-in-java/

UPD:After some thinking, I've decided to create my own class for this: ThreadAffinity.javaIt's JNA-based, and very simple -- so, if you want to use it in production, may be you should spent some time making it more stable, but for benchmarking and testing it works well as is.

UPD:经过一番思考,我决定为此创建自己的类:ThreadAffinity.java它是基于 JNA 的,而且非常简单——所以,如果你想在生产中使用它,可能你应该花一些时间来制作它更稳定,但对于基准测试和测试,它按原样运行良好。

UPD 2:There is another libraryfor working with thread affinity in java. It uses same method as previously noted, but has another interface

UPD 2:还有另一个用于在 java 中处理线程关联。它使用与前面提到的相同的方法,但有另一个接口

回答by questzen

IMO, this will notbe possible unless you use native calls. JVM is supposed to be platform independent, any system calls done to achieve this will not result in a portable code.

IMO,这个不会,除非你使用本地调用是可能的。JVM 应该是平台独立的,为实现这一点而进行的任何系统调用都不会产生可移植的代码。

回答by Hardcoded

It's not possible (at least with plain Java).

这是不可能的(至少对于普通的 Java)。

You can use thread poolsto limit the amount of threads (and therefore cores) used for different types of work, but there is no way to specify a core to use.

您可以使用线程池来限制用于不同类型工作的线程(以及内核)数量,但无法指定要使用的内核。

There is even the (small) possibility that your Java runtime doesn't support native threading for your OS or hardware. In this case, green threadsare used and only one core will be used for the whole JVM.

您的 Java 运行时甚至有(小)可能性不支持您的操作系统或硬件的本机线程。在这种情况下,使用绿色线程,并且整个 JVM 将只使用一个核心。

回答by Dave

I know it's been a while, but if anyone comes across this thread, here's how I solved this problem. I wrote a script that would do the following:

我知道已经有一段时间了,但是如果有人遇到这个线程,这就是我解决这个问题的方法。我写了一个脚本来执行以下操作:

  1. "jstack -l "
  2. Take the results, find the "nid"'s of the threads I want to manually lock down to cores.
  3. Taskset those threads.
  1. “jstack -l”
  2. 获取结果,找到我想手动锁定到内核的线程的“nid”。
  3. Taskset 那些线程。