获取Java虚拟机可用的处理器个数

如何获取Java虚拟机可用的处理器个数?

Runtime类的Native方法#availableProcessors()已经提供了,只要通过Runtme类的实例调用即可。

/**
     * Returns the number of processors available to the Java virtual machine.
     *
     * <p> This value may change during a particular invocation of the virtual
     * machine.  Applications that are sensitive to the number of available
     * processors should therefore occasionally poll this property and adjust
     * their resource usage appropriately. </p>
     *
     * @return  the maximum number of processors available to the virtual
     *          machine; never smaller than one
     * @since 1.4
     */
    public native int availableProcessors();

调用方法:

int availableProcessors = Runtime.getRuntime().availableProcessors();

猜你喜欢

转载自wangmengjun.iteye.com/blog/2326702
今日推荐