Java : ConcurrentModificationException with Examples

ConcurrentModificationException (Java SE 19 & JDK 19) API Examples.
You will find code examples on most ConcurrentModificationException methods.


Summary

This exception may be thrown by methods that have detected concurrent modification of an object when such modification is not permissible.

Class diagram

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 ()

Constructs a ConcurrentModificationException with no detail message.

final var e = new ConcurrentModificationException();
System.out.println(e); // java.util.ConcurrentModificationException

ConcurrentModificationException (String message)

Constructs a ConcurrentModificationException with the specified detail 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)

Constructs a new exception with the specified detail message and 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)

Constructs a new exception with the specified cause and a detail message of (cause==null ? null : cause.toString()) (which typically contains the class and detail message of 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

To top of page