Java : InterruptibleChannel with Examples

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


Summary

A channel that can be asynchronously closed and interrupted.

Class diagram

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

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

            System.out.println("InterruptibleChannel : "
                    + (channel instanceof InterruptibleChannel));

            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 (ClosedByInterruptException e) {
            System.out.println("ClosedByInterruptException!");
        } catch (IOException e) {
            System.out.println("IOException!");
        }
    });

    TimeUnit.SECONDS.sleep(2);

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

// Result
// ↓
//InterruptibleChannel : true
//bind ...
//bind OK!
//accept ...
//-- future.cancel --
//ClosedByInterruptException!
try (final var executor = Executors.newSingleThreadExecutor()) {

    try (final var channel = ServerSocketChannel.open()) {
        executor.submit(() -> {
            try {
                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("-- channel.close --");
    }
}

// Result
// ↓
//bind ...
//bind OK!
//accept ...
//-- channel.close --
//AsynchronousCloseException!

Methods

void close ()

Closes this channel.

final var path = Path.of("R:", "java-work", "test.data");
System.out.println(path); // R:\java-work\test.data

InterruptibleChannel channel;
try (final var fc = FileChannel.open(path,
        StandardOpenOption.CREATE, StandardOpenOption.WRITE)) {

    channel = fc;
    System.out.println(channel.isOpen()); // true
}

System.out.println(channel.isOpen()); // false

An example without a try-with-resources statement.

final var path = Path.of("R:", "java-work", "test.data");
System.out.println(path); // R:\java-work\test.data

final InterruptibleChannel channel = FileChannel.open(path,
        StandardOpenOption.CREATE, StandardOpenOption.WRITE);
try {
    System.out.println(channel.isOpen()); // true
} finally {
    channel.close();
}

System.out.println(channel.isOpen()); // false

Methods declared in Channel

isOpen

Please see the link below.


Related posts

To top of page