Java线程亲和力 [英] Java thread affinity

查看:116
本文介绍了Java线程亲和力的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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中做到这一点。但是如果你真的需要它 - 你可以使用JNI来调用完成工作的本机代码。这是开始的地方:

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:

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/

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

UPD: After some thinking, I've decided to create my own class for this: ThreadAffinity.java It'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 2:还有另一个,用于在java中使用线程关联。它使用与前面提到的相同的方法,但有另一个接口

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

这篇关于Java线程亲和力的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆