Thursday 7 January 2016

Why get method is synchronized in Hashtable?

In my previous post, I explained How Hashtable works in java. I recommend you to go through the post before reading this.

If Hashtable read is not synchronized, then there is a possibility that the Hashtable could be modified during the execution of read. Let's see what happen if get() method of Hashtable is not synchronized. Suppose there are two threads t1, t2 working on same Hashtable instance.

a. Thread t1 is trying to read the value associated with some key 'key1'.

b. Mean time Thread t2, removes/update the value associated with key 'key1'.

c. You are not sure whether thread t1 gets the old value (or) new value (or) null. It causes inconsistency. To overcome these kind of problems get() method is synchronized in Hashtable.


You may like
                                                 Home

No comments:

Post a Comment