Problems with adding a string to a list by multiple threads

advertisements

this is a follow-up post to Do I need a concurrent collection for adding elements to a list by many threads?

everybody there has focused on expaning of the list. I understand how that can be a problem, but .. what about adding elements ? is that thread-safe?

example code

static final Collection<String> FILES = new ArrayList<String>(1000000);

and I execute in many threads (I add less than 1000000 elements)

FILES.add(string)

is that thread safe ? what are possible problems with doing it that way ?


ArrayList<String> is not synchronized by itself. Use Collections.synchronizedList(new ArrayList<String>()) instead.