Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why do I need to synchronize a list returned by Collections.synchronizedList

Tags:

i found this at dos.oracle.com

public static List synchronizedList(List list)

Returns a synchronized (thread-safe) list backed by the specified list. In order to guarantee serial access, it is critical that all access to the backing list is accomplished through the returned list. It is imperative that the user manually synchronize on the returned list when iterating over it:

  List list = Collections.synchronizedList(new ArrayList());       ...   synchronized(list) {       Iterator i = list.iterator(); // Must be in synchronized block       while (i.hasNext())           foo(i.next());   } 

My question is : Why do i have to Synchronize the list to iterate it if Collections.synchronizedList(); is supposed to return an already synchronized list ?

Im just accesing the list in two threads: One Thread just add and the other thread to get and delete. What other classes you recommend to use for this scenario ?

Thanks for reading.

like image 296
GabrielBB Avatar asked Aug 01 '13 12:08

GabrielBB


People also ask

Why is synchronization important in multithreading?

Synchronization in java is the capability to control the access of multiple threads to any shared resource. In the Multithreading concept, multiple threads try to access the shared resources at a time to produce inconsistent results. The synchronization is necessary for reliable communication between threads.

What does synchronized list do?

The synchronizedList() method of java. util. Collections class is used to return a synchronized (thread-safe) list backed by the specified list. In order to guarantee serial access, it is critical that all access to the backing list is accomplished through the returned list.

What are synchronized Collections?

Synchronized Collections. Synchronized collections achieve thread-safety through intrinsic locking, and the entire collections are locked. Intrinsic locking is implemented via synchronized blocks within the wrapped collection's methods.

Why do we need synchronized ArrayList when we have vectors which are synchronized in Java?

Being synchronized means that every operation is thread safe - if you use the same vector from two threads at the same time, they can't corrupt the state. However, this makes it slower. If you are working in a single threaded environment (or the list is limited to a thread and never shared), use ArrayList.


1 Answers

The list being synchronized only means that add, remove etc. operations are synchronized and therefore atomic. Iteration however is not and if a thread adds while another is iterating, you could get a ConcurrentModificationException.

By manually synchronizing your iteration block, you ensure that the list is not modified while iterating.

One alternative is to use a CopyOnWriteArrayList which provides an iterator that iterates over the list as it was known when the iteration started, regardless of subsequent modifications. That collection is however not very efficient if you need to change the content of the list very often.

like image 199
assylias Avatar answered Oct 05 '22 22:10

assylias