Java 如何将JVM绑定到特定的CPU核上

在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 cpus = new ArrayList(); //To save the all cpu of system
  List cpu_set = new ArrayList(); //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核,我将其绑在第三个核上,可以见下图:

Java 如何将JVM绑定到特定的CPU核上_第1张图片
运行后进程是javaw,点鼠标右键出来set affinity就可以看见了。

你可能感兴趣的:(Java 如何将JVM绑定到特定的CPU核上)