Java : BrokenBarrierException with Examples

BrokenBarrierException (Java SE 23 & JDK 23) in Java with Examples.
You will find code samples for most of the BrokenBarrierException methods.


Summary

Exception thrown when a thread tries to wait upon a barrier that is in a broken state, or which enters the broken state while the thread is waiting.

Class diagram

final var barrier = new CyclicBarrier(3);
System.out.println("parties = " + barrier.getParties());

try (final var executor = Executors.newFixedThreadPool(3)) {

    final var futureA = executor.submit(() -> {
        try {
            System.out.println("A : task start");
            barrier.await();
        } catch (InterruptedException | BrokenBarrierException e) {
            System.out.println("A : " + e.getClass().getSimpleName());
        }
    });

    TimeUnit.SECONDS.sleep(1);

    executor.submit(() -> {
        try {
            System.out.println("B : task start");
            barrier.await();
        } catch (InterruptedException | BrokenBarrierException e) {
            System.out.println("B : " + e.getClass().getSimpleName());
        }
    });

    TimeUnit.SECONDS.sleep(1);

    System.out.println("task A cancel!");

    final var ret = futureA.cancel(true);
    System.out.println("cancelled = " + ret);
}

// Result
// ↓
//parties = 3
//A : task start
//B : task start
//task A cancel!
//A : InterruptedException
//B : BrokenBarrierException
//cancelled = true

Constructors

BrokenBarrierException ()

Constructs a BrokenBarrierException with no specified detail message.

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

BrokenBarrierException (String message)

Constructs a BrokenBarrierException with the specified detail message.

final var e = new BrokenBarrierException("abc");
System.out.println(e); // java.util.concurrent.BrokenBarrierException: abc
System.out.println(e.getMessage()); // abc

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