在Java 如何将JVM绑定到特定的CPU核上 ,C,C++都可以实现,那java呢?
要实现这个功能,必须使用JRockit JDK,它完全兼容SUN JDK,因为完全就是一家公司出的嘛。装完之后就有其类库了。
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import com.bea.jvm.*;
public class test1{
public static void main(String[] args) {
int count=0; //Record the count of cpus
JVM jvm = JVMFactory.getJVM();
List<CPU> cpus = new ArrayList<CPU>(); //To save the all cpu of system
List<CPU> cpu_set = new ArrayList<CPU>(); //This cpu is used to bind the JVM
for(Iterator it = JVMFactory.getJVM().getProcessAffinity().iterator();it.hasNext();count++)
{
CPU cpu = (CPU) it.next();
cpus.add(cpu);
}
System.out.println("The count of system cpu is " + count);
cpu_set.add(cpus.get(2));
jvm.suggestProcessAffinity(cpu_set);
while(true);
}
}
我的机子是inte core i5,具有4个CPU核,我将其绑在第三个核上,可以见下图:
![](http://hi.csdn.net/attachment/201108/9/0_1312870555cIl4.gif)