Java : ConcurrentModificationException with Examples
ConcurrentModificationException (Java SE 19 & JDK 19) API Examples.
You will find code examples on most ConcurrentModificationException methods.
Summary
void test(List<Integer> list) throws InterruptedException {
for (int i = 0; i < 5; i++) {
list.add(i);
}
try (final var executor = Executors.newSingleThreadScheduledExecutor()) {
executor.schedule(() -> {
System.out.println("-- add value! --");
list.add(9999);
}, 5, TimeUnit.SECONDS);
try {
for (final var value : list) {
System.out.println("value = " + value);
TimeUnit.SECONDS.sleep(2);
}
} catch (ConcurrentModificationException e) {
System.out.println("ConcurrentModificationException!");
}
}
System.out.println("-- end --");
System.out.println("list = " + list);
}
test(new ArrayList<>());
// Result
// ↓
//value = 0
//value = 1
//value = 2
//-- add value! --
//ConcurrentModificationException!
//-- end --
//list = [0, 1, 2, 3, 4, 9999]
test(new CopyOnWriteArrayList<>());
// Result
// ↓
//value = 0
//value = 1
//value = 2
//-- add value! --
//value = 3
//value = 4
//-- end --
//list = [0, 1, 2, 3, 4, 9999]
Constructors
ConcurrentModificationException ()
final var e = new ConcurrentModificationException();
System.out.println(e); // java.util.ConcurrentModificationException
ConcurrentModificationException (String message)
final var e = new ConcurrentModificationException("abcd");
System.out.println(e); // java.util.ConcurrentModificationException: abcd
System.out.println(e.getMessage()); // abcd
ConcurrentModificationException (String message, Throwable cause)
final var cause = new ConcurrentModificationException("XYZ");
final var e = new ConcurrentModificationException("abcd", cause);
System.out.println(e); // java.util.ConcurrentModificationException: abcd
System.out.println(e.getMessage()); // abcd
System.out.println(e.getCause()); // java.util.ConcurrentModificationException: XYZ
System.out.println(e.getCause().getMessage()); // XYZ
ConcurrentModificationException (Throwable cause)
final var cause = new ConcurrentModificationException("XYZ");
final var e = new ConcurrentModificationException(cause);
// java.util.ConcurrentModificationException: java.util.ConcurrentModificationException: XYZ
System.out.println(e);
// java.util.ConcurrentModificationException: XYZ
System.out.println(e.getMessage());
System.out.println(e.getCause()); // java.util.ConcurrentModificationException: XYZ
System.out.println(e.getCause().getMessage()); // XYZ
Methods declared in Throwable
addSuppressed, fillInStackTrace, getCause, getLocalizedMessage, getMessage, getStackTrace, getSuppressed, initCause, printStackTrace, printStackTrace, printStackTrace, setStackTrace, toString
Please see the link below.
Related posts
- API Examples
- BlockingQueue
- Callable
- CancellationException
- ConcurrentHashMap.KeySetView
- ConcurrentLinkedDeque
- ConcurrentLinkedQueue
- ConcurrentMap
- ConcurrentSkipListSet
- Condition
- CopyOnWriteArrayList
- CopyOnWriteArraySet
- CountDownLatch
- CyclicBarrier
- Exchanger
- Executor
- ExecutorService
- Executors
- Future
- Future.State
- FutureTask
- InterruptedException
- Lock
- Runnable
- Semaphore
- Thread
- ThreadGroup
- ThreadLocal
- TimeUnit