Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Correct way to synchronize ArrayList in java

I'm not sure if this is the correct way to synchronize my ArrayList.

I have an ArrayList in_queue which is passed in from the registerInQueue function.

ArrayList<Record> in_queue = null;  public void registerInQueue(ArrayList in_queue) {     this.in_queue = in_queue; } 

Now I'm trying to synchronize it. Is this sychronizing my in_queue object correctly?

List<Record> in_queue_list = Collections.synchronizedList(in_queue);  synchronized (in_queue_list) {     while (in_queue_list.size() > 0) {         in_queue_list.remove(0);     } } 
like image 443
bob Avatar asked Sep 16 '09 08:09

bob


People also ask

How can we synchronize ArrayList in Java?

In order to get a synchronized list from an ArrayList, we use the synchronizedList(List <T>) method in Java. The Collections. synchronizedList(List <T>) method accepts the ArrayList as an argument and returns a thread safe list.

What is synchronization ArrayList?

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.

Are ArrayList methods synchronized?

Implementation of ArrayList is not synchronized by default. It means if a thread modifies it structurally and multiple threads access it concurrently, it must be synchronized externally.

Can we synchronized ArrayList?

ArrayList class can be used to increase the capacity of an ArrayList instance, if necessary, to ensure that it can hold at least the number of elements specified by the minimum capacity argument.


2 Answers

You're synchronizing twice, which is pointless and possibly slows down the code: changes while iterating over the list need a synchronnization over the entire operation, which you are doing with synchronized (in_queue_list) Using Collections.synchronizedList() is superfluous in that case (it creates a wrapper that synchronizes individual operations).

However, since you are emptying the list completely, the iterated removal of the first element is the worst possible way to do it, sice for each element all following elements have to be copied, making this an O(n^2) operation - horribly slow for larger lists.

Instead, simply call clear() - no iteration needed.

Edit: If you need the single-method synchronization of Collections.synchronizedList() later on, then this is the correct way:

List<Record> in_queue_list = Collections.synchronizedList(in_queue); in_queue_list.clear(); // synchronized implicitly,  

But in many cases, the single-method synchronization is insufficient (e.g. for all iteration, or when you get a value, do computations based on it, and replace it with the result). In that case, you have to use manual synchronization anyway, so Collections.synchronizedList() is just useless additional overhead.

like image 94
Michael Borgwardt Avatar answered Sep 28 '22 03:09

Michael Borgwardt


Looking at your example, I think ArrayBlockingQueue (or its siblings) may be of use. They look after the synchronisation for you, so threads can write to the queue or peek/take without additional synchronisation work on your part.

like image 40
Brian Agnew Avatar answered Sep 28 '22 04:09

Brian Agnew