Java开发技巧

Java开发技巧

1.分段查询

java 复制代码
//查询商品信息
List<SkuRes> allskuResList = new ArrayList<>();
	Lists.partition(customsGoodsIdList, 1000).forEach(o -> {
     Map<String, Object> itemParams = new HashMap<>();
     itemParams.put("qp-id-in", StringUtils.join(o, ","));
     List<SkuRes> skuResList = skuAdapterService.listNoPage(itemParams);
     if (CollectionUtils.isNotEmpty(skuResList)) {
         allskuResList.addAll(skuResList);
     }
 });
List<SkuRes> skuResList = allskuResList.stream().distinct().collect(Collectors.toList());

2.explain sql执行计划

sql 复制代码
explain (analyze, verbose, costs, buffers, timing)
SELECT * FROM `table`

3.CompletableFuture异步测试

java 复制代码
    public static void main(String[] args) {
        LocalDateTime now = LocalDateTime.now();
        long milliseconds = now.atZone(ZoneId.systemDefault()).toInstant().toEpochMilli();
        ExecutorService customExecutor = Executors.newFixedThreadPool(10);
        log.info("异步开始时间: " + now);
        List<CompletableFuture<String>> completableFutures = new ArrayList<>();
        for (int i = 0; i < 1000; i++) {
            CompletableFuture<String> future = CompletableFuture.supplyAsync(() -> {
                FtpInfoEntity ftpInfoEntity = FtpInfoEntity.builder().ftpHost("121.32.135.90").ftpAccount("yunpan").ftpPwd("tongDan359-4yTkeM").ftpPort(20201).build();
                FtpUtils.connectCheck(ftpInfoEntity.getFtpHost(), ftpInfoEntity.getFtpPort(), ftpInfoEntity.getFtpAccount(), ftpInfoEntity.getFtpPwd());
                return Thread.currentThread().getName() + " 连接成功";

            }, customExecutor).exceptionally(ex -> {
                // 处理异常
                log.info("连接异常: " + ex.getMessage());
                return Thread.currentThread().getName() + " 连接异常";
            });
            completableFutures.add(future);

        }
        // 等待异步方法执行完成
        Supplier<Stream<CompletableFuture<String>>> streamCopier = () -> Optional.ofNullable(completableFutures)
                .map(Collection::stream)
                .orElse(Stream.empty())
                .filter(Objects::nonNull);

        CompletableFuture<List<String>> result = CompletableFuture.allOf(completableFutures.toArray(new CompletableFuture<?>[0]))
                .thenApply(v -> streamCopier.get()
                        .map(CompletableFuture::join)
                        .collect(toList())
                );

        streamCopier.get().forEach(f -> f.whenComplete((t, ex) -> {
            if (ex != null) {
                log.error("CompletableFuture exception", ex);
                result.completeExceptionally(ex);
            }
        }));

        LocalDateTime now1 = LocalDateTime.now();
        long milliseconds1 = now1.atZone(ZoneId.systemDefault()).toInstant().toEpochMilli();
        log.info("异步截至时间: " + now1 + "运行时间: " + (milliseconds1 - milliseconds));
    }
相关推荐
李少兄2 小时前
CentOS系统下前后端项目部署攻略
linux·运维·centos
Sylvia-girl3 小时前
Java——抽象类
java·开发语言
Two_brushes.5 小时前
【Linux】线程机制深度实践:创建、等待、互斥与同步
linux·运维·服务器·多线程
江沉晚呤时6 小时前
在 C# 中调用 Python 脚本:实现跨语言功能集成
python·microsoft·c#·.net·.netcore·.net core
设计师小聂!6 小时前
Linux系统中部署Redis详解
linux·运维·数据库·redis
kfepiza6 小时前
Debian-10编译安装Mysql-5.7.44 笔记250706
linux·数据库·笔记·mysql·debian·bash
Touper.6 小时前
Redis 基础详细介绍(Redis简单介绍,命令行客户端,Redis 命令,Java客户端)
java·数据库·redis
m0_535064607 小时前
C++模版编程:类模版与继承
java·jvm·c++
电脑能手7 小时前
如何远程访问在WSL运行的Jupyter Notebook
ide·python·jupyter
虾条_花吹雪7 小时前
Using Spring for Apache Pulsar:Message Production
java·ai·中间件