Why hashtable is synchronized in java




















Iterator in the HashMap is fail-safe while the enumerator for the Hashtable is not. Fail-safe is relevant from the context of iterators. If an iterator has been created on a collection object and some other thread tries to modify the collection object "structurally" , a concurrent modification exception will be thrown. Hashtable uses Seperate chaining with linked lists as collision handling strategy bounding search time of O n.

From JDK 1. Home C VB. Performance HashMap is not synchronized so it is fast. Fail-safe Iterator in the HashMap is fail-safe while the enumerator for the Hashtable is not. Time Complexity Hashtable uses Seperate chaining with linked lists as collision handling strategy bounding search time of O n. HashMap Implementation import java. Entry m:days.

What gives Java its 'write once and run anywhere' nature? What is JVM and is it platform independent? What is the garbage collector in Java? What is numeric promotion? Why do we need Generic Types in Java?

The following example shows how to synchronize a Hashtable , determine if a Hashtable is synchronized, and use a synchronized Hashtable.

The Synchronized method is thread safe for multiple readers and writers. Furthermore, the synchronized wrapper ensures that there is only one writer writing at a time. Enumerating through a collection is intrinsically not a thread-safe procedure. Even when a collection is synchronized, other threads can still modify the collection, which causes the enumerator to throw an exception.

There are some similar but different weird constraints when you use iterators on synchronized wrappers from Collections. The javadoc says:. Pure Danger Tech navigation home. Entry m:hm. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

Skip to content. Change Language. Related Articles. Table of Contents. Save Article. Improve Article.



0コメント

  • 1000 / 1000