Concurrency in Java is easy, but it's tricky. If you don't use it wisely, you will end up in a mess. The other important thing is to know about the features which Java provides out of the box.
Hash map is handy, we all use it. To use it in multi threaded applications Java provides synchronized hash map, which is a wrapper for hash map.
Synchronized hash map uses the lock on the whole map, which can impact performance when multiple threads access the map too often and spend time to acquire the lock. Data structure for map is a table with hashing and chaining. So, when one thread is trying to put a value in map, it would access one row (the whole chain in that row) in table. Locking only that particular row will help, but synchronized map will lock the whole map, hence block all other threads.
Java also provides concurrent hash map, which uses strip locking, which is , lock the row of the table and not the whole table. Hence multiple threads can access the map without any concurrency problem.
Friday, July 25, 2008
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment