Example usage for java.lang Runtime availableProcessors

List of usage examples for java.lang Runtime availableProcessors

Introduction

In this page you can find the example usage for java.lang Runtime availableProcessors.

Prototype

public native int availableProcessors();

Source Link

Document

Returns the number of processors available to the Java virtual machine.

Usage

From source file:Main.java

public static void main(String[] args) {

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

From source file:Main.java

public static void main(String[] args) {
    Runtime runtime = Runtime.getRuntime();

    int nrOfProcessors = runtime.availableProcessors();

    System.out.println("Number of processors available to the Java Virtual Machine: " + nrOfProcessors);
}

From source file:org.lenskit.cli.Main.java

public static void main(String[] args) {
    ArgumentParser parser = ArgumentParsers.newArgumentParser("lenskit")
            .description("Work with LensKit recommenders and data.");
    Logging.addLoggingGroup(parser);/*from  ww w  .jav a2s  .c om*/

    Subparsers subparsers = parser.addSubparsers().metavar("COMMAND").title("commands");
    ServiceLoader<Command> loader = ServiceLoader.load(Command.class);
    for (Command cmd : loader) {
        Subparser cp = subparsers.addParser(cmd.getName()).help(cmd.getHelp()).setDefault("command", cmd);
        cmd.configureArguments(cp);
    }

    try {
        Namespace options = parser.parseArgs(args);
        Logging.configureLogging(options);
        Runtime rt = Runtime.getRuntime();
        logger.info("Starting LensKit {} on Java {} from {}", LenskitInfo.lenskitVersion(),
                SystemUtils.JAVA_VERSION, SystemUtils.JAVA_VENDOR);
        logger.debug("Built from Git revision {}", LenskitInfo.getHeadRevision());
        logger.debug("Using VM '{}' version {} from {}", SystemUtils.JAVA_VM_NAME, SystemUtils.JAVA_VM_VERSION,
                SystemUtils.JAVA_VM_VENDOR);
        logger.info("Have {} processors and heap limit of {} MiB", rt.availableProcessors(),
                rt.maxMemory() >> 20);
        Command cmd = options.get("command");
        cmd.execute(options);
        logger.info("If you use LensKit in published research, please see http://lenskit.org/research/");
    } catch (ArgumentParserException e) {
        parser.handleError(e);
        System.exit(1);
    } catch (Exception e) {
        logger.error("error running command: " + e, e);
        System.exit(2);
    }
}

From source file:de.langmi.spring.batch.examples.complex.crosscutting.autothreadconf.AsyncTaskExecutorFactory.java

public static SimpleAsyncTaskExecutor createInstance() {
    SimpleAsyncTaskExecutor instance = new SimpleAsyncTaskExecutor();

    // set concurrencyLimit according to available processors
    Runtime runtime = Runtime.getRuntime();
    int nrCpu = runtime.availableProcessors();
    instance.setConcurrencyLimit(nrCpu);

    LOG.info("TaskExecutor ConcurrencyLimit:" + String.valueOf(nrCpu));

    return instance;
}

From source file:edu.stanford.mobisocial.dungbeetle.ui.adapter.ObjectListCursorAdapter.java

private static int getBestBatchSize() {
    Runtime runtime = Runtime.getRuntime();
    if (runtime.availableProcessors() > 1)
        return 100;

    try {/*  ww  w. j  a  v a  2s .c  om*/
        File max_cpu_freq = new File("/sys/devices/system/cpu/cpu0/cpufreq/cpuinfo_max_freq");
        byte[] freq_bytes = IOUtils.toByteArray(new FileInputStream(max_cpu_freq));
        String freq_string = new String(freq_bytes);
        double freq = Double.valueOf(freq_string);
        if (freq > 950000) {
            return 50;
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
    return 15;
}

From source file:org.eclipse.wb.internal.core.editor.errors.report2.EnvironmentFileReportInfo.java

private static String createContents(IProject project) {
    String c = "";
    c += "Product Name: " + BrandingUtils.getBranding().getProductName() + CR;
    c += "Product Version: " + ProductInfo.getProduct().getVersion() + "[" + ProductInfo.getProduct().getBuild()
            + "]" + CR;
    c += "Installation Path: " + getInstallationPath() + CR;
    c += "Eclipse Version: " + PlatformInfo.getEclipseVersion().toString() + CR;
    c += "Eclipse Build Name: " + PlatformInfo.getEclipseBuildName() + CR;
    c += "Eclipse Build ID: " + PlatformInfo.getEclipseBuildId() + CR;
    c += "IDE Name: " + PlatformInfo.getIDEName() + CR;
    c += "IDE Version: " + PlatformInfo.getIDEVersionString() + CR;
    c += "IDE NL: " + PlatformInfo.getIDENL() + CR;
    c += "Eclipse Commands: " + StringUtils.replaceChars(getSystemProperty("eclipse.commands"), "\n\r", " ")
            + CR;/*from  w ww .  java 2s  . co m*/
    c += "Eclipse VM: " + getSystemProperty("eclipse.vm") + CR;
    c += "Eclipse VM Args: " + getSystemProperty("eclipse.vmargs") + CR;
    c += "OS Name: " + getSystemProperty("os.name") + CR;
    c += "OS Arch: " + getSystemProperty("os.arch") + CR;
    c += "OS Version: " + getSystemProperty("os.version") + CR;
    String linuxDescription = getLinuxDescription();
    if (!StringUtils.isEmpty(linuxDescription)) {
        c += "Linux Description: " + linuxDescription + CR;
    }
    String m_mozillaResult = tryCreateMozilla();
    if (!StringUtils.isEmpty(m_mozillaResult)) {
        c += "Browser Creation Result: " + m_mozillaResult + CR;
    }
    Runtime runtime = Runtime.getRuntime();
    c += "Available Processors: " + runtime.availableProcessors() + CR;
    c += "Memory Max: " + runtime.maxMemory() + CR;
    c += "Memory Total: " + runtime.totalMemory() + CR;
    c += "Memory Free: " + runtime.freeMemory() + CR;
    c += "Java Vendor: " + getSystemProperty("java.vendor") + CR;
    c += "Java Version: " + getSystemProperty("java.version") + CR;
    c += "Java Library Path: " + CR + getSystemProperty("java.library.path") + CR;
    c += "Project Class Path: " + CR + getClassPath(project) + CR;
    return c;
}

From source file:com.jaspersoft.studio.community.utils.CommunityAPIUtils.java

/**
 * @return software and hardware info./*from   w ww.  java 2  s . c om*/
 */
public static String getHardwareSoftwareInfo() {
    BrandingInfo currBranding = JaspersoftStudioPlugin.getInstance().getBrandingInformation();
    String c = ""; //$NON-NLS-1$
    c += "Product Name: " + currBranding.getProductName() + CR; //$NON-NLS-1$
    c += "Product Version: " + currBranding.getProductVersion() + CR; //$NON-NLS-1$
    c += "Installation Path: " + getInstallationPath(currBranding.getProductMainBundleID()) + CR; //$NON-NLS-1$
    c += "Eclipse Version: " + PlatformInfo.getEclipseVersion().toString() //$NON-NLS-1$
            + CR;
    c += "Eclipse Build Name: " + PlatformInfo.getEclipseBuildName() + CR; //$NON-NLS-1$
    c += "Eclipse Build ID: " + PlatformInfo.getEclipseBuildId() + CR; //$NON-NLS-1$
    c += "IDE Name: " + PlatformInfo.getIDEName() + CR; //$NON-NLS-1$
    c += "IDE Version: " + PlatformInfo.getIDEVersionString() + CR; //$NON-NLS-1$
    c += "IDE NL: " + PlatformInfo.getIDENL() + CR; //$NON-NLS-1$
    c += "Eclipse Commands: " //$NON-NLS-1$
            + StringUtils.replaceChars(getSystemProperty("eclipse.commands"), "\n\r", " ") //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
            + CR;
    c += "Eclipse VM: " + getSystemProperty("eclipse.vm") + CR; //$NON-NLS-1$ //$NON-NLS-2$
    c += "Eclipse VM Args: " + getSystemProperty("eclipse.vmargs") + CR; //$NON-NLS-1$ //$NON-NLS-2$
    c += "OS Name: " + getSystemProperty("os.name") + CR; //$NON-NLS-1$ //$NON-NLS-2$
    c += "OS Arch: " + getSystemProperty("os.arch") + CR; //$NON-NLS-1$ //$NON-NLS-2$
    c += "OS Version: " + getSystemProperty("os.version") + CR; //$NON-NLS-1$ //$NON-NLS-2$
    String linuxDescription = getLinuxDescription();
    if (!StringUtils.isEmpty(linuxDescription)) {
        c += "Linux Description: " + linuxDescription + CR; //$NON-NLS-1$
    }
    String m_mozillaResult = tryCreateMozilla();
    if (!StringUtils.isEmpty(m_mozillaResult)) {
        c += "Browser Creation Result: " + m_mozillaResult + CR; //$NON-NLS-1$
    }
    Runtime runtime = Runtime.getRuntime();
    c += "Available Processors: " + runtime.availableProcessors() + CR; //$NON-NLS-1$
    c += "Memory Max: " + runtime.maxMemory() + CR; //$NON-NLS-1$
    c += "Memory Total: " + runtime.totalMemory() + CR; //$NON-NLS-1$
    c += "Memory Free: " + runtime.freeMemory() + CR; //$NON-NLS-1$
    c += "Java Vendor: " + getSystemProperty("java.vendor") + CR; //$NON-NLS-1$ //$NON-NLS-2$
    c += "Java Version: " + getSystemProperty("java.version") + CR; //$NON-NLS-1$ //$NON-NLS-2$
    c += "Java Library Path: " + getSystemProperty("java.library.path") + CR; //$NON-NLS-1$ //$NON-NLS-2$
    return c;
}

From source file:org.apache.solr.handler.admin.SystemInfoHandler.java

/**
 * Get JVM Info - including memory info//from  w w w.  j a v  a 2 s .c o  m
 */
public static SimpleOrderedMap<Object> getJvmInfo() {
    SimpleOrderedMap<Object> jvm = new SimpleOrderedMap<Object>();
    jvm.add("version", System.getProperty("java.vm.version"));
    jvm.add("name", System.getProperty("java.vm.name"));

    Runtime runtime = Runtime.getRuntime();
    jvm.add("processors", runtime.availableProcessors());

    long used = runtime.totalMemory() - runtime.freeMemory();
    // not thread safe, but could be thread local
    DecimalFormat df = new DecimalFormat("#.#");
    double percentUsed = ((double) (used) / (double) runtime.maxMemory()) * 100;

    SimpleOrderedMap<Object> mem = new SimpleOrderedMap<Object>();
    mem.add("free", humanReadableUnits(runtime.freeMemory(), df));
    mem.add("total", humanReadableUnits(runtime.totalMemory(), df));
    mem.add("max", humanReadableUnits(runtime.maxMemory(), df));
    mem.add("used", humanReadableUnits(used, df) + " (%" + df.format(percentUsed) + ")");
    jvm.add("memory", mem);

    // JMX properties -- probably should be moved to a different handler
    SimpleOrderedMap<Object> jmx = new SimpleOrderedMap<Object>();
    try {
        RuntimeMXBean mx = ManagementFactory.getRuntimeMXBean();
        jmx.add("bootclasspath", mx.getBootClassPath());
        jmx.add("classpath", mx.getClassPath());

        // the input arguments passed to the Java virtual machine
        // which does not include the arguments to the main method.
        jmx.add("commandLineArgs", mx.getInputArguments());
        // a map of names and values of all system properties.
        //jmx.add( "SYSTEM PROPERTIES", mx.getSystemProperties());

        jmx.add("startTime", new Date(mx.getStartTime()));
        jmx.add("upTimeMS", mx.getUptime());
    } catch (Exception e) {
        log.warn("Error getting JMX properties", e);
    }
    jvm.add("jmx", jmx);
    return jvm;
}

From source file:com.searchbox.framework.web.SystemController.java

@ModelAttribute("jvmInfo")
public static Map<String, Object> getJvmInfo() {
    Map<String, Object> jvm = new HashMap<String, Object>();

    final String javaVersion = System.getProperty("java.specification.version", "unknown");
    final String javaVendor = System.getProperty("java.specification.vendor", "unknown");
    final String javaName = System.getProperty("java.specification.name", "unknown");
    final String jreVersion = System.getProperty("java.version", "unknown");
    final String jreVendor = System.getProperty("java.vendor", "unknown");
    final String vmVersion = System.getProperty("java.vm.version", "unknown");
    final String vmVendor = System.getProperty("java.vm.vendor", "unknown");
    final String vmName = System.getProperty("java.vm.name", "unknown");

    // Summary Info
    jvm.put("version", jreVersion + " " + vmVersion);
    jvm.put("name", jreVendor + " " + vmName);

    // details//  ww w.  jav a 2  s  .  c  o  m
    Map<String, Object> java = new HashMap<String, Object>();
    java.put("vendor", javaVendor);
    java.put("name", javaName);
    java.put("version", javaVersion);
    jvm.put("spec", java);
    Map<String, Object> jre = new HashMap<String, Object>();
    jre.put("vendor", jreVendor);
    jre.put("version", jreVersion);
    jvm.put("jre", jre);
    Map<String, Object> vm = new HashMap<String, Object>();
    vm.put("vendor", vmVendor);
    vm.put("name", vmName);
    vm.put("version", vmVersion);
    jvm.put("vm", vm);

    Runtime runtime = Runtime.getRuntime();
    jvm.put("processors", runtime.availableProcessors());

    // not thread safe, but could be thread local
    DecimalFormat df = new DecimalFormat("#.#", DecimalFormatSymbols.getInstance(Locale.ROOT));

    Map<String, Object> mem = new HashMap<String, Object>();
    Map<String, Object> raw = new HashMap<String, Object>();
    long free = runtime.freeMemory();
    long max = runtime.maxMemory();
    long total = runtime.totalMemory();
    long used = total - free;
    double percentUsed = ((double) (used) / (double) max) * 100;
    raw.put("free", free);
    mem.put("free", humanReadableUnits(free, df));
    raw.put("total", total);
    mem.put("total", humanReadableUnits(total, df));
    raw.put("max", max);
    mem.put("max", humanReadableUnits(max, df));
    raw.put("used", used);
    mem.put("used", humanReadableUnits(used, df) + " (%" + df.format(percentUsed) + ")");
    raw.put("used%", percentUsed);

    mem.put("raw", raw);
    jvm.put("memory", mem);

    // JMX properties -- probably should be moved to a different handler
    Map<String, Object> jmx = new HashMap<String, Object>();
    try {
        RuntimeMXBean mx = ManagementFactory.getRuntimeMXBean();
        jmx.put("bootclasspath", mx.getBootClassPath());
        jmx.put("classpath", mx.getClassPath());

        // the input arguments passed to the Java virtual machine
        // which does not include the arguments to the main method.
        jmx.put("commandLineArgs", mx.getInputArguments());

        jmx.put("startTime", new Date(mx.getStartTime()));
        jmx.put("upTimeMS", mx.getUptime());

    } catch (Exception e) {
        LOGGER.warn("Error getting JMX properties", e);
    }
    jvm.put("jmx", jmx);
    return jvm;
}

From source file:com.bstek.dorado.console.utils.SystemUtils.java

/**
 * ??/*  w w w  .  j  av  a2s  .co m*/
 * 
 * @return
 */
public static Map<String, Object> getMemoryInfo() {
    Map<String, Object> map = new HashMap<String, Object>();
    Runtime runtime = Runtime.getRuntime();
    map.put("runtime", runtime);

    map.put("freeMemory", runtime.freeMemory());
    map.put("totalMemory", runtime.totalMemory());
    if (System.getProperty("java.version").compareTo("1.4") >= 0)
        map.put("maxMemory", runtime.maxMemory());
    else
        map.put("maxMemory", "N/A");

    map.put("CPU", runtime.availableProcessors());
    return map;
}