16 August 2005

  • ConcurrentHashMap -- High performance thread-aware map. A simple test which inserts 10000 Integer for each threads shows that:
    Type No of threads Average Time (sec)
    ConcurrentHashMap 2 0.06
    Synchronized HashMap 2 0.06
    ConcurrentHashMap 10 0.18
    Synchronized HashMap 10 0.59
    ConcurrentHashMap 20 0.25
    Synchronized HashMap 20 1.15

    more thread, more performance gain

  • CopyOnWriteArrayList, CopyOnWriteArraySet -- A thread-aware List/Set optimize for read. DO NOT use these for a lots of write operations !
  • Choose the right collection:

    Type Eliminate
    Duplication
    Auto-Sorted Random write Random read Maintain
    Insertion Order
    Note
    ArrayList X X OK for < 1000 O O choose this first
    PriorityQueue X O X X X Not allow null
    LinkedList X X O (fast sort) poor O (Queue) allow null
    HashMap O (key) X O O X WeakHashMap for cache
    TreeMap O (key) O O O X with Comparator
    LinkedHashMap O (key) X O O O
    HashSet O X X X X
    TreeSet O O X X X with Comparator
    LinkedHashSet O X X X O

  • Never use Vector, Stack, and Hashtable.

To be continue...


回響

可以用 Tag <I>、<B>,程式碼請用 <PRE>