2009年3月2日 星期一

Thread Safe Collections

Thread Safe Collections //ref from: http://en.wikibooks.org/wiki/Java:Collections

It is also called Concurrent Collections. Most of the popular collection classes have implementations for both single thread and multiple thread environments. The non-syncronized implementations are always faster. You can use the non-syncronized implementations in multiple thread environments, when you make sure that only one thread updating the collection an any given time.
A new Java JDK package was introduced at Java 1.5, that is java.util.concurrent. This package supplies a few Collection implementations designed for use in multithreaded environments.
The following table list all the syncronized collection classes


syncronizednon-syncronized
Listjava.util.Vectorjava.util.ArrayList
java.util.Stack
java.util.LinkList
java.util.concurrent.CopyOnWriteArrayList
Setjava.util.TreeSet
java.util.HashSet
java.util.LinkHashSet
java.util.concurrent.CopyOnWriteArraySet
Mapjava.util.TreeMap
java.util.Hashtable

java.util.concurrent.ConcurrentHashMap

java.util.HashMap
java.util.LinkHashMap
java.util.IdentityHashMap
java.util.EnumMap

the default size of Vector, Arraylist, Hashtable, Hashmap, Hashset in Java

Vector - 10, Arraylist - 10, Hashtable - 11, Hashmap - 16, Hashset - 16