I have a pre-populated array list. And I have multiple threads which will remove elements from the array list. Each thread calls the remove method below and removes one item
Yes, Just be careful if you are also iterating over the list, because in this case you will need to synchronize on it. From the Javadoc:
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());
}
Or, you can use CopyOnWriteArrayList
which is slower for writes but doesn't have this issue.
Yes, it will work fine as you have synchronized
the list . I would suggest you to use CopyOnWriteArrayList
.
CopyOnWriteArrayList<String> cpList=new CopyOnWriteArrayList<String>(new ArrayList<String>());
void remove(String item)
{
do something; (doesn't work on the list)
cpList..remove(item);
}
It will give consistent behavior for add/remove operations. But while iterating you have to explicitly synchronized. Refer this link
synchronized(list) {
for (Object o : list) {}
}
That should be fine as long as you don't require the "remove" method to be atomic.
In other words, if the "do something" checks that the item appears more than once in the list for example, it is possible that the result of that check will be wrong by the time you reach the next line.
Also, make sure you synchronize on the list when iterating:
synchronized(list) {
for (Object o : list) {}
}
As mentioned by Peter Lawrey, CopyOnWriteArrayList can make your life easier and can provide better performance in a highly concurrent environment.
You can have 2 diffent problems with lists :
1) If you do a modification within an iteration even though in a mono thread environment, you will have ConcurrentModificationException like in this following example :
List<String> list = new ArrayList<String>();
for (int i=0;i<5;i++)
list.add("Hello "+i);
for(String msg:list)
list.remove(msg);
So, to avoid this problem, you can do :
for(int i=list.size()-1;i>=0;i--)
list.remove(i);
2)The second problem could be multi threading environment. As mentioned above, you can use synchronized(list) to avoid exceptions.