Example usage for java.lang.management ThreadMXBean setThreadCpuTimeEnabled

List of usage examples for java.lang.management ThreadMXBean setThreadCpuTimeEnabled

Introduction

In this page you can find the example usage for java.lang.management ThreadMXBean setThreadCpuTimeEnabled.

Prototype

public void setThreadCpuTimeEnabled(boolean enable);

Source Link

Document

Enables or disables thread CPU time measurement.

Usage

From source file:eu.planets_project.tb.gui.backing.exp.ExpTypeMigrate.java

public static void main(String args[]) {

    Properties p = System.getProperties();

    ByteArrayOutputStream byos = new ByteArrayOutputStream();
    try {/*from w w w. j a  va2 s  .c o  m*/
        p.storeToXML(byos, "Automatically generated for PLANETS Service ", "UTF-8");
        String res = byos.toString("UTF-8");
        System.out.println(res);
    } catch (IOException e) {
        e.printStackTrace();
    }

    // This.
    List<String> pl = new ArrayList<String>();
    for (Object key : p.keySet()) {
        pl.add((String) key);
    }
    Collections.sort(pl);

    //
    for (String key : pl) {
        System.out.println(key + " = " + p.getProperty(key));
    }

    /*
     * http://java.sun.com/j2se/1.5.0/docs/api/java/lang/management/ThreadMXBean.html#getCurrentThreadCpuTime()
     * 
     * http://www.java-forums.org/new-java/5303-how-determine-cpu-usage-using-java.html
     * 
     */

    ThreadMXBean TMB = ManagementFactory.getThreadMXBean();
    int mscale = 1000000;
    long time = 0, time2 = 0;
    long cput = 0, cput2 = 0;
    double cpuperc = -1;

    //Begin loop.
    for (int i = 0; i < 10; i++) {

        if (TMB.isThreadCpuTimeSupported()) {
            if (!TMB.isThreadCpuTimeEnabled()) {
                TMB.setThreadCpuTimeEnabled(true);
            }

            //                if(new Date().getTime() * mscale - time > 1000000000) //Reset once per second
            //                {
            System.out.println("Resetting...");
            time = System.currentTimeMillis() * mscale;
            cput = TMB.getCurrentThreadCpuTime();
            //                cput = TMB.getCurrentThreadUserTime();
            //                }

        }

        //Do cpu intensive stuff
        for (int k = 0; k < 10; k++) {
            for (int j = 0; j < 100000; j++) {
                double a = Math.pow(i, j);
                double b = a / j + Math.random();
                a = b * Math.random();
                b = a * Math.random();
            }

            try {
                Thread.sleep(10);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

        }

        if (TMB.isThreadCpuTimeSupported()) {
            //                if(new Date().getTime() * mscale - time != 0) {
            cput2 = TMB.getCurrentThreadCpuTime();
            System.out.println("cpu: " + (cput2 - cput) / (1000.0 * mscale));
            //                cput2 = TMB.getCurrentThreadUserTime();

            time2 = System.currentTimeMillis() * mscale;
            System.out.println("time: " + (time2 - time) / (1000.0 * mscale));

            cpuperc = 100.0 * (cput2 - cput) / (double) (time2 - time);
            System.out.println("cpu perc = " + cpuperc);
            //                }
        }
        //End Loop
    }
    System.out.println("Done.");
}

From source file:io.pcp.parfait.benchmark.CPUThreadTest.java

private void runBenchmark(boolean cpuTracingEnabled, CPUThreadTestRunner.CpuLookupMethod cpuLookupMethod) {
    ExecutorService executorService = Executors.newFixedThreadPool(numThreads);
    List<CPUThreadTestRunner> executions = newArrayList();

    ThreadMXBean threadBean = ManagementFactory.getThreadMXBean();
    threadBean.setThreadCpuTimeEnabled(cpuTracingEnabled);

    threadBean.setThreadContentionMonitoringEnabled(true);

    long begin = currentTimeMillis();

    for (int i = 0; i < numThreads; i++) {
        CPUThreadTestRunner cpuThreadTestRunner = new CPUThreadTestRunner(iterations, cpuLookupMethod);
        executorService.execute(cpuThreadTestRunner);
        executions.add(cpuThreadTestRunner);
    }/*from w  w w. ja va2s.c o  m*/

    awaitExecutionCompletion(executorService);

    long end = currentTimeMillis();
    long timeTakenms = end - begin;

    report(executions, timeTakenms, iterations, cpuTracingEnabled, cpuLookupMethod);
}