Java运行时availableProcessors()方法



描述

Java Runtime availableProcessors() 方法返回 Java 虚拟机可用的处理器数量。此值在虚拟机的特定调用期间可能会发生变化。因此,对可用处理器数量敏感的应用程序应定期轮询此属性并相应地调整其资源使用情况。

声明

以下是java.lang.Runtime.availableProcessors() 方法的声明

public int availableProcessors()

参数

返回值

此方法返回虚拟机可用的最大处理器数;绝不小于一个

异常

示例:获取环境的可用处理器

以下示例演示了 Java Runtime availableProcessors() 方法的用法。我们使用 availableProcessors() 方法打印了当前环境中可用处理器的数量。

package com.tutorialspoint;

public class RuntimeDemo {


   public static void main(String[] args) {
  
      // print a normal message
      System.out.println("Hello world!");

      // check the number of processors available
      System.out.println(Runtime.getRuntime().availableProcessors());
   }
}

输出

让我们编译并运行上述程序,这将产生以下结果:

Hello world!
32
java_lang_runtime.htm
广告
© . All rights reserved.