JAvaにおけるList,Set,Map,Queueツールクラスのスレッドの問題
Collections Concurrent java <? extends Set> <? extends Collection> ,
, 。 java , 3 ,Map,List,Set。
JDK 1.2 Collection , List、Set Map。 JDK (HashMap、Hashtable、TreeMap、WeakHashMap、HashSet、TreeSet、Vector、ArrayList、LinkedList )。 (Hashtable Vector), (Collections.synchronizedMap()、synchronizedList() synchronizedSet()), 。
java.util.concurrent (ConcurrentHashMap、CopyOnWriteArrayList CopyOnWriteArraySet)。 、 、 。
java.util 。 , , , , ConcurrentModificationException 。( ; , 。) , , 。java.util.concurrent 。
JDK 5.0 -- Queue BlockingQueue。Queue List , , 。 List , ArrayList LinkedList Queue 。 List , Queue List, 。
CopyOnWriteArrayList CopyOnWriteArraySet
List -- Vector ArrayList Collections.synchronizedList()。java.util.concurrent CopyOnWriteArrayList。 List ? Vector ?
。 Vector List , fail-fast , List, 。
Vector 。 , , 。 ConcurrentModificationException, , , 。
CopyOnWriteArrayList , , 。 , , , , 。
Set , List, Set -- CopyOnWriteArraySet。
ConcurrentHashMap
List , 、 hash Map -- Hashtable, Collections.synchronizedMap() HashMap。JDK 5.0 ConcurrentHashMap , Map , 。
Hashtable synchronizedMap ( Hashtable Map ) 。 , , hash 。 , , 。 get() put() , , put-if-absent( ), , 。
Hashtable Collections.synchronizedMap 。 Map , Map , , 。
,ConcurrentHashMap , , 。 Map , 。
,ConcurrentHashMap Hashtable Collections.synchronizedMap(new HashMap()) 。 , , ConcurrentHashMap 。 , ConcurrentHashMap , 。 , ( ) , put-if-absent。ConcurrentHashMap , ConcurrentModificationException , " " Map 。
:List、Map Set。List , -- 、 。
LinkedList ( ) 。 ,List , , 。 List LinkedList 。Queue List , put() take() , LinkedList 。
Queue 。ConcurrentLinkedQueue (first-in-first-out,FIFO) , PriorityQueue ( ), , 。
interface Queue extends Collection {
boolean offer(E x);
E poll();
E remove() throws NoSuchElementException;
E peek();
E element() throws NoSuchElementException;
}
Queue :
• LinkedList Queue。
• PriorityQueue ( ) , 。
• ConcurrentLinkedQueue 、 、 FIFO 。
Hashtable ConcurrentHashMap
,ConcurrentHashMap Hashtable 。Hashtable Map;ConcurrentHashMap , , 。 , , ConcurrentHashMap Hashtable 。
Hashtable ConcurrentHashMap 。 ,N , Hashtable ConcurrentHashMap ,60% put() ,2% remove() 。 Linux Xeon 。 10,000,000 , ConcurrentHashMap, 。 ,ConcurrentHashMap , Hashtable 。
, 。 , , , 。
この文書の内容は次のとおりです.http://www.cnblogs.com/sarafill/archive/2011/05/18/2049461.html
まとめ:具体的には、Vector,Hashtableは操作上はスレッドが安全であるが、遍歴スレッドでは安全ではない.同理Collections.synchronizedMap()、synchronizedList()およびsynchronizedSet()は、前者よりも性能が向上するが、遍歴は前者と同様にロックする必要がある.ConcurrentHashMap、CopyOnWriteArrayListおよびCopyOnWriteArraySet、ConcurrentLinkedQueue パフォーマンスとスレッドセキュリティが大幅に改善され、スレッドセキュリティが遍歴しています.