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

查看:28
本文介绍了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天全站免登陆