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));
    }
相关推荐
a95114164211 分钟前
SQL触发器实现自动生成流水号_配合序列对象实现递增逻辑
jvm·数据库·python
敖正炀14 分钟前
集合-Set深入解析
java
哦哦~92116 分钟前
FDTD 与 Python 联合仿真的超表面智能设计技术与应用
python·fdtd·超表面
财经资讯数据_灵砚智能16 分钟前
基于全球经济类多源新闻的NLP情感分析与数据可视化(夜间-次晨)2026年4月21日
人工智能·python·信息可视化·自然语言处理·ai编程
解救女汉子22 分钟前
mysql如何配置元数据锁超时_mysql lock_wait_timeout设置
jvm·数据库·python
小此方25 分钟前
Re:Linux系统篇(四)指令篇 · 三:两套打包压缩重要指令方法+剩余指令收尾
linux·运维·服务器
下次再写28 分钟前
Java互联网大厂面试技术问答实战:涵盖Java SE、Spring Boot、微服务及多场景应用
java·数据库·缓存·面试·springboot·microservices·技术问答
公众号-老炮说Java30 分钟前
IDEA 2026.1 + Claude Code = 降维打击
java·ide·intellij-idea
千寻girling35 分钟前
RabbitMQ 详细教程(38K字数)
java·后端·面试
冷风阿健36 分钟前
Linux 源码安装 Vim 9.2.0272 完整步骤(修复 CVE-2026-34714)
linux·运维·vim