idea警告Possibly blocking call in non-blocking context could lead to thread starvation

看下列代码

这个警告的说在非阻塞上下文中阻塞调用可能会导致线程饥饿

解决方法:


 private Mono<Path> createTempFile(String name) {
        String prefix = name +"_";
        return Mono.defer(() -> {
            Path dir1 = Path.of(dir);
           return isExist(dir1).flatMap(path -> createFile(path, prefix, ".jpg"));
        }).subscribeOn(Schedulers.boundedElastic());
    }

    private Mono<Path> isExist(Path path){
        return Mono.fromCallable(()->{
            if(Files.notExists(path)){
                return  Files.createDirectories(path);
            }
            return path;
        }).subscribeOn(Schedulers.boundedElastic());
    }

    private Mono<Path> createFile(Path path, String prefix, String suffix){
        return Mono.fromCallable(()->Files.createTempFile(path,prefix, ".jpg"))
                .subscribeOn(Schedulers.boundedElastic());
    }


来自为知笔记(Wiz)


posted on 2023-04-11 04:49  白衣风云  阅读(2501)  评论(0编辑  收藏  举报

导航