Skip to content

Instantly share code, notes, and snippets.

@afinlay5
Created August 24, 2021 14:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save afinlay5/270a62364d57116de22d89789167b79b to your computer and use it in GitHub Desktop.
Save afinlay5/270a62364d57116de22d89789167b79b to your computer and use it in GitHub Desktop.
ConcurrentModificationException
import java.util.ConcurrentModificationException;
import java.util.ArrayList;
public class CMETest {
public static void main (String[] args) {
ArrayList<Integer> numbers = new ArrayList<>();
int qty = Integer.MAX_VALUE/20;
for (int i=0; i<qty; i++) {
numbers.add(i);
}
Thread thrd1 = new Thread( () -> {
numbers.add(0);
numbers.remove(1);
});
Thread thrd2 = new Thread( () -> {
try {
for (int i : numbers) {
}
} catch (ConcurrentModificationException cme) {
cme.printStackTrace();
Thread.getAllStackTraces().keySet().forEach(thread -> {
String toPrint = thread.getName() + "\t\tSTATE: " + thread.getState()
+ "\t\tINTERRUPTED?" + (thread.isInterrupted() ? "Y" : "N");
System.out.println(toPrint);
});
}
});
thrd2.start(); thrd1.start();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment