Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

About this user

Kevin Gaudin http://myspace.com/nivekbass

« Newer Snippets
Older Snippets »
Showing 1-2 of 2 total  RSS 

Which class file is loaded by the classloader ?

When using lots of third-party libraries, one problem might be that 2 of them package different versions of the same class, producing errors when method version conflicts happen.

Here is a simple way to know the exact location used by the classloader to get your class :

URL myClassURL = MyMysteryClass.class.getProtectionDomain().getCodeSource().getLocation();

StopWatch - Why is my app so slow ???

StopWatch is a small class allowing to manage multiple concurrent stopwatches to measure time elapsed between two calls. Nothing is done while time is rolling (no thread, no loop), it only stores and compares timestamps.

import java.util.Hashtable;

public class StopWatch {
    private static final Hashtable startTime = new Hashtable();
    
    public static void start(String id){
        startTime.put(id,new Long(System.currentTimeMillis()));
    }

    public static long stop(String id){
        return System.currentTimeMillis() - ((Long)startTime.remove(id)).longValue();
    }
}


Example of use :
public main(String[] args) {
    // Start a global stopwatch
    StopWatch.start("GLOBAL");

    // evaluate time used by task 1
    StopWatch.start("TASK1");
    executeTask1();
    System.out.println("Time elapsed for task 1 : " + StopWatch.stop("TASK1") + "ms";

    // evaluate time used by task 2
    StopWatch.start("TASK2");
    executeTask2();
    System.out.println("Time elapsed for task 2 : " + StopWatch.stop("TASK2") + "ms";

    // Display time elapsed for full processing
    System.out.println("Total processing time : " + StopWatch.stop("GLOBAL") + "ms";
}
« Newer Snippets
Older Snippets »
Showing 1-2 of 2 total  RSS