知识卡片

结构图卡 · 生成模型:未记录

完成回调的成功与失败

理解等级:中级

回调式把结果交给CompletionHandler:completed接收成功结果,failed接收Throwable和附着对象。附着对象携带调用者提供的上下文,不是失败异常本身。原书把失败签名写成failed(V,A)并暗示回到主线程,均不准确:回调线程由通道相关线程池安排,不能把它当作UI或业务主线程。

结构图: 类图:实线空心三角表示接口继承,虚线空心三角表示实现,虚线箭头表示调用依赖;只保留本卡相关成员。

classDiagram
class CompletionHandler {
<<interface>>
+completed(V result, A attachment) void
+failed(Throwable exc, A attachment) void
}
class AsynchronousFileChannel {
+read(ByteBuffer dst, long position, A attachment, CompletionHandler handler) void
}
AsynchronousFileChannel ..> CompletionHandler : 分发结果
这张卡片看不懂?

已有反馈 · 0 / 10 条

    还没有人提交反馈。

    在整书笔记中的位置

    原书出处 · 1 处

    参考来源

    - 位置:《Java程序员修炼之道》第2章《新I/O》2.5(来源项目book-sources;源文件:extracted/Java程序员修炼之道/_epub-src/text/part0032.html) - 结论依据:原文展开了本卡机制与应用;错误签名和线程/通道语义经Java7官方文档更正(2026-11-06):https://docs.oracle.com/javase/7/docs/api/java/nio/channels/AsynchronousFileChannel.html ; https://docs.oracle.com/javase/7/docs/api/java/util/concurrent/Future.html ; https://docs.oracle.com/javase/7/docs/api/java/nio/channels/CompletionHandler.html - 原始内容:ad [" + result + "]"); } public void failed(Throwable exception, ByteBuffer attachment) { System.out.println(exception.getMessage()); } }); } catch (IOE