I am using SynchronizedCollection.containsAll problem is facing is when i ever i run the following code i am getting ConcurrentModification Exception.
From my understanding code should terminate without any exception.
public class Main {
public static void main(String[] args) {
List l1 = new LinkedList(), l2 = new LinkedList();
for (int i = 0; i < 100000; i++) {
l1.add("" + i);
}
for (int i = 0; i < 100000; i++) {
l2.add("" + i);
}
// reverse to make the search take a little longer
Collections.reverse(l2);
final List sl1 = Collections.synchronizedList(l1);
final List sl2 = Collections.synchronizedList(l2);
new Thread() {
public void run() {
// synchronized (sl2) {
sl1.containsAll(sl2);
// }
}
}.start();
new Thread() {
public void run() {
try {
Thread.sleep(100);
} catch (InterruptedException e) {
}
sl2.add("3");
}
}.start();
}
}
Can some one help me understand why i am getting this exception.
Collections.synchronizedList()'s javadoc ?Thread.sleep(100);change it toThread.sleep(1000);and you will see