As we already know Collections which is nothing but collections of Objects where we deals with the Objects using some pre-defined methods. But There are several problems which occurs when we use Collections concept in multi-threading. The problems which occurs while using Collections in Multi-threaded application:
- Most of the Collections classes objects (like ArrayList, LinkedList, HashMap etc) are non-synchronized in nature i.e. multiple threads can perform on a object at a time simultaneously. Therefore objects are not thread-safe.(大多数的集合类都不是线程安全的)
- Very few Classes objects (like Vector, Stack, HashTable) are synchronized in nature i.e. at a time only one thread can perform on an Object. But here the problem is performance is low because at a time single thread execute an object and rest thread has to wait.
(虽然Vector,Stack,或者 HashTable是线程安全的,但是他们在同一时间只允许一个线程访问,其他的线程就会被lock住,在性能上不好) - The main problem is when one thread is iterating an Collections object then If another thread try to modify the content of object then we will get RuntimeException saying ConcurrentModificationException.
(当一个线程在使用iterator遍历一个集合的时候,如果集合的内容被其他线程修改,就会抛出并发修改的异常,这也令人头疼)
Because of the above reason Collections classes is not suitable or we can say that good choice for Multi-threaded applications.
To overcome the above problem SUN microSystem introduced a new feature in JDK 1.5Version, which is nothing but Concurrent Collections.