Java : AbstractInterruptibleChannel 示例

Java 中的 AbstractInterruptibleChannel (Java SE 22 & JDK 22) 及其示例。
您将找到大多数 AbstractInterruptibleChannel 方法的代码示例。

注解 :

  • 本文可能使用了翻译软件以方便阅读。 另请查看英文原文

简介

可中断通道的基本实现类。 (机器翻译)

Class diagram

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

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

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

            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
// ↓
//AbstractInterruptibleChannel : 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!

Constructors

AbstractInterruptibleChannel ()

初始化此类的新实例。 (机器翻译)

protected. I think it's rare to create a subclass of this class. Therefore, the code example is omitted.

Methods

protected final void begin ()

标记可能无限期阻塞的 I/O 操作的开始。 (机器翻译)

protected. I think it's rare to create a subclass of this class. Therefore, the code example is omitted.

final void close ()

关闭此频道。 (机器翻译)

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

AbstractInterruptibleChannel 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 AbstractInterruptibleChannel channel = FileChannel.open(path,
        StandardOpenOption.CREATE, StandardOpenOption.WRITE);
try {
    System.out.println(channel.isOpen()); // true
} finally {
    channel.close();
}

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

protected final void end (boolean completed)

标记可能无限期阻塞的 I/O 操作的结束。 (机器翻译)

protected. I think it's rare to create a subclass of this class. Therefore, the code example is omitted.

protected abstract void implCloseChannel ()

关闭此频道。 (机器翻译)

protected. I think it's rare to create a subclass of this class. Therefore, the code example is omitted.

final boolean isOpen ()

告知该通道是否打开。 (机器翻译)

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

AbstractInterruptibleChannel 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 AbstractInterruptibleChannel channel = FileChannel.open(path,
        StandardOpenOption.CREATE, StandardOpenOption.WRITE);
try {
    System.out.println(channel.isOpen()); // true
} finally {
    channel.close();
}

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

相关文章

To top of page