36

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);
    }
}
WoLfPwNeR
  • 1,148
  • 4
  • 11
  • 27
bob
  • 407
  • 1
  • 5
  • 9
  • 5
    You can't really upgrade a list to be synchronized because you are being passed a reference, so you don't really own it. Whatever gave you the reference could still modify the original list it passed you concurrently. Regardless of what synchronization you add, unless it is explicitly known that the list will be guarded by it's intrinsic lock. – Tim Bender Sep 16 '09 at 08:46
  • I will be putting a synchronized block around any operation on the queue. Thanks! – bob Sep 16 '09 at 09:09

5 Answers5

49

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.

Michael Borgwardt
  • 342,105
  • 78
  • 482
  • 720
  • 8
    Synchronizing twice here is not pointless: it ensures that while the loop is running nobody else can modify the list. Not using `clear()` is a bit over-the-top, though. :) – Bombe Sep 16 '09 at 08:43
  • so I should do something like: synchronized ((List)in_queue) ? – bob Sep 16 '09 at 08:45
  • Ok! I actually removed a bit of code to make it simple. I won't have a problem with clear()/remove(). thanks =] – bob Sep 16 '09 at 08:49
  • 4
    @Bombe: no, it is completely pointless. As I wrote, the use of Collections.synchronizedList() is superfluous – Michael Borgwardt Sep 16 '09 at 11:20
  • When I try manual synchronization I get an error message "field not static final" when I use synchronized (in_queue). Should this be replaced with synchronized ((List)in_queue) ? – bob Sep 16 '09 at 12:17
  • 1
    No, that won't achieve anything. What is the exact error message and at what line does it occur? The error message "field not static final" can't be found via Google, so I doubt that's what you actually get. – Michael Borgwardt Sep 16 '09 at 13:28
8

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.

Brian Agnew
  • 268,207
  • 37
  • 334
  • 440
  • Thanks for the suggestion! That is what I am trying to do, not sure about limiting the size of my array though. I'll keep this in mind. ;) – bob Sep 16 '09 at 09:06
  • Note there's a LinkedBlockingQueue as well. And you don't necessarily need to impose limits. – Brian Agnew Sep 16 '09 at 09:13
6

That's correct, and documented:

http://java.sun.com/javase/6/docs/api/java/util/Collections.html#synchronizedList(java.util.List)

However, to clear the list, just call List.clear().

Andrew Duffy
  • 6,800
  • 2
  • 23
  • 17
5

Yes it is the correct way, but the synchronised block is required if you want all the removals together to be safe - unless the queue is empty no removals allowed. My guess is that you just want safe queue and dequeue operations, so you can remove the synchronised block.

However, there are far advanced concurrent queues in Java such as ConcurrentLinkedQueue

David Rabinowitz
  • 29,904
  • 14
  • 93
  • 125
1

Let's take a normal list (implemented by the ArrayList class) and make it synchronized. This is shown in the SynchronizedListExample class. We pass the Collections.synchronizedList method a new ArrayList of Strings. The method returns a synchronized List of Strings. //Here is SynchronizedArrayList class

package com.mnas.technology.automation.utility;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import org.apache.log4j.Logger;
/**
* 
* @author manoj.kumar
* @email kumarmanoj.mtech@gmail.com
* 
*/
public class SynchronizedArrayList {
    static Logger log = Logger.getLogger(SynchronizedArrayList.class.getName());
    public static void main(String[] args) {    
        List<String> synchronizedList = Collections.synchronizedList(new ArrayList<String>());
        synchronizedList.add("Aditya");
        synchronizedList.add("Siddharth");
        synchronizedList.add("Manoj");
        // when iterating over a synchronized list, we need to synchronize access to the synchronized list
        synchronized (synchronizedList) {
            Iterator<String> iterator = synchronizedList.iterator();
            while (iterator.hasNext()) {
                log.info("Synchronized Array List Items: " + iterator.next());
            }
        }    
    }
}

Notice that when iterating over the list, this access is still done using a synchronized block that locks on the synchronizedList object. In general, iterating over a synchronized collection should be done in a synchronized block

Dhaval Patel
  • 10,119
  • 5
  • 43
  • 46