Wednesday 13 December 2017

Difference: ArrayList vs CopyOnWriteArrayList in java

ArrayList and CopyOnWriteArrayList in java differ based on following parameters:

1. Inclusion in java

Arraylist was added in Java 1.2 version.

CopyOnWriteArrayList was added in Java 1.5 or Java 5.

2. Thread-safety

Arraylist in java is not synchronized thus is not at all thread safe.

CopyOnWriteArrayList is synchronized in nature. Thus it is thread safe and for the same reason only one thread can access the resources in this class at a time.

3. Performance

As arraylist is non-synchronized so it is better performance wise as multiple threads can access the resources of arraylist at the same time simultaneously.

Being synchronized means only one thread can have access to CopyOnWriteArrayList resources thus impacting its performance speed.

4. ConcurrentModificationException

Arraylist throws ConcurrentModificationException.

CopyOnWriteArrayList doesnot throw ConcurrentModificationException.

5. Fail fast or fail safe

 Arraylist's  iterator and list iterator are fail fast in nature.

CopyOnWriteArrayList iterator are fail safe in nature.

You may also like to read:

No comments:

Post a Comment

If you are looking for a reference book on java then we recommend you to go for → Java The Complete Reference
Click on the image link below to get it now.