真的Files.createDirectory()是非阻塞的吗?

时间:2018-11-22 18:33:03

标签: java nio project-reactor

我已经创建了这样的方法:

Flux<GetObjectResponse> download(String bucket, List<String> s3FileNames) {
    String tmpDirName = UUID.randomUUID().toString();
    Path dir = Paths.get(tmpDirName);
    if (!Files.exists(dir)) {
        try {
            Files.createDirectory(dir);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    return Flux.fromIterable(s3FileNames)
            .flatMap(filename -> Mono.just(GetObjectRequest.builder().bucket(bucket).key(filename).build()))
            .flatMap(getObjectRequest -> Mono.fromFuture(s3AsyncClient.getObject(getObjectRequest, Paths.get(tmpDirName, getObjectRequest.key()))));
}

稍后我会处理此响应流,但是当我调用Files.createDirectory(dir)时,IntelliJ会给我一个提示:

Inappropriate blocking method call less... (Ctrl+1) 
Inspection info: Reports thread-blocking method calls found in a code fragment where a thread should not be blocked

IntelliJ对吗?与文件互操作时NIO是否阻塞?

1 个答案:

答案 0 :(得分:1)

NIO2之前的所有文件操作都是同步的,您可以假定该方法返回时操作已完成。 只有TCP套接字和文件监视程序具有异步操作。

NIO2添加了AsynchronousFileChannel,但不适用于目录。