FileDocCategorySizeDatePackage
HashTest.javaAPI DocExample2950Thu Mar 11 19:44:26 GMT 2004javathreads.examples.ch14

HashTest

public class HashTest extends Object implements Runnable

Fields Summary
static int
nLoops
static int
nThreads
static Map
targetMap
Constructors Summary
Methods Summary
static voidcleanGC()

        System.gc();
        System.runFinalization();
        System.gc();
    
static voiddoTest(java.util.Map m)

	targetMap = m;
	Thread[] t = new Thread[nThreads];
	Runnable r = new HashTest();
	for (int i = 0; i < nThreads; i++)
	    t[i] = new Thread(r);
	for (int i = 0; i < nThreads; i++)
	    t[i].start();
	for (int i = 0; i < nThreads; i++)
	    try { t[i].join(); } catch (Exception e) {}
    
public static voidmain(java.lang.String[] args)


	nLoops = 1000000;
        nThreads = 1;
        doTest(new Hashtable());
        doTest(new HashMap());
        doTest(new ConcurrentHashMap());
	nLoops = Integer.parseInt(args[0]);

        System.out.println("Starting standard hashmap test");
	cleanGC();
	Timestamp hashTS = new Timestamp();
        doTest(new HashMap());
	hashTS.stop();
        System.out.println("Standard hashmap (1 thread) took " + hashTS);

        System.out.println("Starting standard hashtable test");
	cleanGC();
	Timestamp hashtableTS = new Timestamp();
        doTest(new Hashtable());
	hashtableTS.stop();
        System.out.println("Standard hashtable (1 thread) took " + hashtableTS);

        System.out.println("Starting concurrent hashmap test");
	cleanGC();
	Timestamp concTS = new Timestamp();
        doTest(new ConcurrentHashMap());
	concTS.stop();
        System.out.println("Concurrent hashmap (1 thread) took " + concTS);

	nThreads = Integer.parseInt(args[1]);

        System.out.println("Starting standard hashtable test");
	cleanGC();
	Timestamp hashNTS = new Timestamp();
        doTest(new Hashtable());
	hashNTS.stop();
        System.out.println("Standard hashtable (" + nThreads + " threads) took " + hashNTS);

        System.out.println("Starting concurrent hashmap test");
	cleanGC();
	Timestamp concNTS = new Timestamp();
        doTest(new ConcurrentHashMap());
	concNTS.stop();
        System.out.println("Concurrent hashmap (" + nThreads + " threads) took " + concNTS);

	double d = ((double) (hashtableTS.elapsedTime() - hashTS.elapsedTime())) /
		nLoops;
	System.out.println("Hashmap saves " + d + " " + hashTS.units() + " per call vs. hashtable");

	d = ((double) (hashTS.elapsedTime() - concTS.elapsedTime())) /
		nLoops;
	System.out.println("Unsynchronized operation (1 thread) saves " + d + " " + concTS.units() + " per call");

	d = ((double) (hashNTS.elapsedTime() - concNTS.elapsedTime())) /
		(nLoops * nThreads);
	System.out.println("Unsynchronized operation (" + nThreads + " threads) saves " + d + " " + concNTS.units() + " per call");
    
public voidrun()

	for (int i = 0; i < nLoops; i++) {
	    Integer I = new Integer(i);
	    targetMap.put(I, I);
	    targetMap.get(I);
	}