【发布时间】:2022-01-30 00:21:11
【问题描述】:
我刚刚发现下面的代码没有按预期工作。
try (AsynchronousFileChannel channel = open(path)) {
channel.read(, , ,
new CompletionHandler() {
@Override
public void completed(Integer result, Long attachment) {
}
@Override
public void failed(Throwable exc, Long attachment) {
// channel already has been closed!!!
}
});
}
因为AsynchronousFileChannel#read 方法会立即返回,而CompletionHandler 只是无法工作。
任务完成后关闭频道的常用成语是什么?
我应该这样做吗?
AsynchronousFileChannel channel = open(path);
channel.read(, , ,
new CompletionHandler() {
@Override
public void completed(Integer result, Long attachment) {
}
@Override
public void failed(Throwable exc, Long attachment) {
}
});
channel.close();
【问题讨论】:
标签: java asynchronous asynchronousfilechannel