Java : AsynchronousCloseException with Examples

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


Summary

Checked exception received by a thread when another thread closes the channel or the part of the channel upon which it is blocked in an I/O operation.

Class diagram

try (final var executor = Executors.newSingleThreadExecutor()) {

    final var future = executor.submit(() -> {
        try (final var channel = ServerSocketChannel.open()) {

            System.out.println("bind ...");
            channel.bind(new InetSocketAddress("127.0.0.1", 8000));
            System.out.println("bind OK!");

            System.out.println("accept ...");
            channel.accept();
            System.out.println("accept OK!");

        } catch (AsynchronousCloseException e) {
            System.out.println("AsynchronousCloseException!");
        } catch (IOException e) {
            System.out.println("IOException!");
        }
    });

    TimeUnit.SECONDS.sleep(2);

    System.out.println("-- future.cancel --");
    future.cancel(true);
}

// Result
// ↓
//bind ...
//bind OK!
//accept ...
//-- future.cancel --
//AsynchronousCloseException!

Constructors

AsynchronousCloseException ()

Constructs an instance of this class.

final var e = new AsynchronousCloseException();
System.out.println(e); // java.nio.channels.AsynchronousCloseException

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