How can I extract these similar codes into only one method? I have implemented three ways of Singleton idiom in Java, and now I want to write some test code to see there performance. Here is my code:

public static void main(String...args) {
    long startTime, endTime;
    int MAX_ITE = Integer.MAX_VALUE;

    // Eagerly initialized static instance guarantees thread safety.
    startTime = getCurrentTime();
    for(int i = 0; i &lt; MAX_ITE; i++) {
        IvoryTower.getInstance();
    }
    endTime = getCurrentTime();
    System.out.println("Hungry initialization time:" + (endTime - startTime));

    // Double check locking, thread safe.
    startTime = getCurrentTime();
    for (int i = 0; i &lt; MAX_ITE; i++) {
        ThreadSafeDoubleCheckLocking.getInstance();
    }
    endTime = getCurrentTime();
    System.out.println("Lazy initialization time:" + (endTime - startTime));

    // On demand holder
    startTime = getCurrentTime();
    for (int i =  0; i &lt; MAX_ITE; i++) {
        InitializationOnDemandHolder.getInstance();
    }
    endTime = getCurrentTime();
    System.out.println("On demand holder time:" + (endTime - startTime));
}


These blocks of codes are very similar, but since getInstance() is static, I don't know how to extract each block into a same method call. Can anyone help?
Simple, with a bit of overhead, calling a run()  

    testInstance("IT", () -&gt; IvoryTower.getInstance());
    testInstance("TSDCL", () -&gt; ThreadSafeDoubleCheckLocking.getInstance());
    testInstance("IODH", () -&gt; InitializationOnDemandHolder.getInstance());
}

private void testInstance(String title, Runnable r) {
    long startTime = getCurrentTime();
    for (int i =  0; i &lt; MAX_ITE; i++) {
        r.run();
    }
    long endTime = getCurrentTime();
    System.out.println(title + ": " + (endTime - startTime));
}


Possibly with testInstance as lambda too if you do not want a second method.

OR

Runnable[] rs = {
    () -&gt; IvoryTower.getInstance(),
    () -&gt; ThreadSafeDoubleCheckLocking.getInstance(),
    () -&gt; InitializationOnDemandHolder.getInstance()
};
for (r : rs) {
    ...
    r.run();
    ...
}