I have an ArrayList
of String
s and two threads are concurrently accessing the list.
What will the output of following snippet and why?
public static void main(String[] args) {
final ArrayList<String> list = new ArrayList<String>();
for (int i = 0; i < 100; i++) {
list.add("Number" + i);
}
new Thread() {
public void run() {
for (String s : list) {
System.out.println(s);
}
}
}.start();
new Thread() {
public void run() {
list.remove("Number5");
}
}.start();
}
I tried using the same code making the Arraylist
synchronized using Collections.synchronizedList(list)
. It is still throwing java.util.ConcurrentModificationException
.