简体   繁体   中英

Is java.util.Hashtable thread safe?

It's been a while since I've used hashtable for anything significant, but I seem to recall the get() and put() methods being synchronized.

The JavaDocs don't reflect this. They simply say that the class Hashtable is synchronized. What can I assume? If several threads access the hashtable at the same time (assuming they are not modifying the same entry), the operations will succeed, right? I guess what I'm asking is "Is java.util.Hashtable thread safe?"

Please Guide me to get out of this issue...

It is threadsafe because the get, put, contains methods etc are synchronized. Furthermore, Several threads will not be able to access the hashtable at the same time, regardless of which entries they are modifying.

edit - amended to include the provisio that synchronization makes the hashtable internally threadsafe in that it is modified atomically; it doesn't guard against race conditions in outside code caused by concurrent access to the hashtable by multiple threads.

For general usage it is thread safe.

But you have to understand that it doesent make your application logic around it thread-safe. For eg consider implementing to put a value in a map, if its not there already. This idiom is called putIfAbsent. Its difficult to implement this in a thread-safe manner using HashTable alone. Similarly for the idiom replace(k,V,V).

Hence for certain idioms like putIfAbsent and and replace(K,V,V) , I would recommend using ConcurrentHashMap

Hashtable is deprecated. Forget it. If you want to use synchronized collections, use Collections.syncrhonize*() wrapper for that purpose. But these ones are not recommended. In Java 5, 6 new concurrent algorithms have been implemented. Copy-on-write, CAS, lock-free algorithms. For Map interface there are two concurrent implementations. ConcurrentHashMap (concurrent hash map) and ConcurrentSkipListMap - concurrent sorted map implementaion.

The first one is optimized for reading, so retrievals do not block even while the table is being updated. Writes are also work much faster comparing with synchronized wrappers cause a ConcurrentHashMap consists of not one but a set of tables, called segments. It can be managed by the last argument in the constructor:

public ConcurrentHashMap(int initialCapacity,
                         float loadFactor,
                         int concurrencyLevel);

ConcurrentHashMap is indispensable in highly concurrent contexts, where it performs far better than any available alternative.

I'm asking is "Is java.util.Hashtable thread safe?".

Yes Hashtable is thread safe, If a thread safe is not needed in your application then go through HashMap, In case, If a thread-safe implementation is desired,then it is recommended to use ConcurrentHashMap in place of Hashtable.

No. It is 'threadsafe' only to the extent that its methods are synchronized. However it is not threadsafe in general, and it can't be, because classes that export internal state such as Iterators or Enumerations require the use of the internal state to be synchronized as well. That's why the new Collections classes are not synchronized, as the Java designers recognized that thread-safety is up to the user of the class, not the class itself.

Note, that a lot of the answers state that Hashtable is synchronised. but this will give you a very little. The synchronization is on the accessor / mutator methods will stop two threads adding or removing from the map concurrently, but in the real world you will often need additional synchronisation.

Even iterating over a Hashtable's entries is not thread safe unless you also guard the Map from being modified through additional synchronization.

If you look into Hashtable code, you will see that methods are synchronized such as:

public synchronized V get(Object key) 
 public synchronized V put(K key, V value)
 public synchronized boolean containsKey(Object key)

You can keep pressing on control key (command for mac) and then click on any method name in the eclipse to go to the java source code.

Unlike the new collection implementations, Hashtable is synchronized. * If a thread-safe implementation is not needed, it is recommended to use HashMap * in place of Hashtable. If a thread-safe highly-concurrent implementation is desired, then it is recommended to use ConcurrentHashMap in place of Hashtable.

http://download.oracle.com/javase/7/docs/api/java/util/Hashtable.html

Yes, Hashtable thread safe, so only one thread can access a hashtable at any time

HashMap, on the other side, is not thread safe (and thus 'faster').

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM