I have just found HiddenInterator example, in the Java concurrency in practice book.
class ConcurentIterator implements Runnable {
// because of final it is immutable, so it is thread safe
private final Set<Integer> v = new HashSet<Integer>();
//private final Vector<Integer> v = new Vector<Integer>();
public synchronized void add(Integer i) {
v.add(i);
}
public synchronized void remove(Integer i) {
v.remove(i);
}
public void addTenThings() {
Random r = new Random();
for (int i = 0; i < 10; i++) {
add(r.nextInt());
System.out.println("DEBUG: added ten elements to " + v);
}
}
public void run() {
addTenThings();
}
}
I understand everything! that's nice!
I have the following question:
In java we have concurrent collections for instance: ConcurentSkipListMap, ConcurrentHashMap, where the problem is fixed. But I'm interested which are the classes where the problem can happen(where it is not fixed)? For instance can it be occur on vector? When I was testing, I can't throw the ConcurentModificationException in the vector.
Testing method:
public static void main(String[] args) {
Thread[] threadArray = new Thread[200];
ConcurentIterator in = new ConcurentIterator();
for (int i = 0; i < 100; i++) {
threadArray[i] = new Thread(in);
}
for (int i = 0; i < threadArray.length; i++) {
threadArray[i].start();
}
}