Java Synchronized list
Solution 1:
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.
Solution 2:
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.
Solution 3:
From Collections#synchronizedList(List)
javadoc
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. Failure to follow this advice may result in non-deterministic behavior.
Solution 4:
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.