How to improve performance of a Hashtable used in multi threaded Java application running in HP-unix box?

In my multi threaded Java application running in HP-Unix, i have a hastable. The size of the hashtable is not constant. It is populated from oracle DB using a select query once when the application is loaded.Then i parse this hashtable and process for each entry in it. Now, when i have close to 15K entries in this hashtable, the get() is very slow. How to improve this get() functionality.

I don't know what version of Java you are running, but consider ConcurrentHashMap. It was added in Java 1.5, or for older versions of Java, there is a library that contains it. http://gee.cs.oswego.edu/dl/classes/EDU/oswego/cs/dl/util/concurrent/intro.html

Also I _highly_ recommend this book: http://www.amazon.com/Java-Concurrency-Practice-Brian-Goetz/dp/0321349601

ConcurrentHashMap allows multiple threads to read simultaneously without blocking which will greatly improve the speed. Older stuff like Hashtable has synchronized on every method, so only a single thread can read at a time. I'm not exactly sure about Hashtable's synchronized implementation, but I believe that is true.

Browse around at the IBM developerworks site I'm including the link to and you'll find some great articles on this topic.

2 Responses

  1. Aoqua Says:

    Since you're processing every element in sequence I'm not sure a Hashtable is your best choice. Hashtables are optimized for random access by key.

    You say your app is multithreaded, but if you're sequentially processing the Hashtable it sounds like only one thread is doing that, so there are no synchronization issues.

    I'd be inclined to try a few other options for speed:
    * use an ArrayList instead of a Hashtable if you're just doing sequential processing and never need to access by key.
    * try a HashMap instead of a Hashtable if you need to access by key as well. Although it's much the same in principle it is a newer collection than Hashtable and may give better performance.
    * if you do need to access by key occasionally but the sequential processing is the main thing you could even try a pair of arrays, one for the key and the other for the full entry. You could sequentially process the second array very quickly and you could write a small method that did a binary search on the first when finding a key (see Arrays.binarySearch(a,k)) to get the index to use for the second array. Crude, but quick.

    I can't promise any of these methods will be faster, but they shouldn't take too long to try out. You might get a pleasant surprise.
    References :

  2. Jim C Says:

    I don't know what version of Java you are running, but consider ConcurrentHashMap. It was added in Java 1.5, or for older versions of Java, there is a library that contains it. http://gee.cs.oswego.edu/dl/classes/EDU/oswego/cs/dl/util/concurrent/intro.html

    Also I _highly_ recommend this book: http://www.amazon.com/Java-Concurrency-Practice-Brian-Goetz/dp/0321349601

    ConcurrentHashMap allows multiple threads to read simultaneously without blocking which will greatly improve the speed. Older stuff like Hashtable has synchronized on every method, so only a single thread can read at a time. I'm not exactly sure about Hashtable's synchronized implementation, but I believe that is true.

    Browse around at the IBM developerworks site I'm including the link to and you'll find some great articles on this topic.
    References :
    http://gee.cs.oswego.edu/dl/classes/EDU/oswego/cs/dl/util/concurrent/intro.html
    http://www.ibm.com/developerworks/library/j-jtp08223/

Leave a Comment

Please note: Comment moderation is enabled and may delay your comment. There is no need to resubmit your comment.

Posted on August 14th, 2008 by admin and filed under unix for |

|