Created
August 24, 2021 14:54
-
-
Save afinlay5/270a62364d57116de22d89789167b79b to your computer and use it in GitHub Desktop.
ConcurrentModificationException
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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