benchmark Callable task - Java java.util.concurrent

Java examples for java.util.concurrent:Callable

Description

benchmark Callable task

Demo Code


//package com.java2s;
import java.util.concurrent.Callable;

public class Main {
    private static final Runtime runtime = Runtime.getRuntime();

    public static void bench(final String name, final Callable<?> callable) {
        System.gc();//from   w ww  .ja v a  2s.c  om
        try {
            final long stMemory = runtime.totalMemory()
                    - runtime.freeMemory();
            final long stTime = System.currentTimeMillis();
            final Object o = callable.call();
            final long enTime = System.currentTimeMillis();
            final long enMemory = runtime.totalMemory()
                    - runtime.freeMemory();
            System.gc();
            final long retMemory = runtime.totalMemory()
                    - runtime.freeMemory();
            System.err.println(o.hashCode());
            System.out.printf("%s\t: %fs, %fM, %fM\n", name,
                    (enTime - stTime) * 1e-3, (enMemory - stMemory) * 1e-6,
                    (retMemory - stMemory) * 1e-6);
        } catch (Exception e) {
            e.printStackTrace();
        }
        System.gc();
    }
}

Related Tutorials