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));
    }
相关推荐
王小王-12333 分钟前
基于Python的二手车价格影响因素研究【多种机器学习对比、线性回归、MLP、SVR、LightGBM】
python·机器学习·汽车·二手车价格预测·二手车价格影响·汽车数据分析
三木水1 小时前
Spring-rabbit使用实战七
java·分布式·后端·spring·消息队列·java-rabbitmq·java-activemq
java1234_小锋1 小时前
一周学会Matplotlib3 Python 数据可视化-绘制饼状图(Pie)
开发语言·python·信息可视化
别来无恙1491 小时前
Spring Boot文件下载功能实现详解
java·spring boot·后端·数据导出
optimistic_chen1 小时前
【Java EE初阶 --- 网络原理】JVM
java·jvm·笔记·网络协议·java-ee
朱小弟cs61 小时前
Orange的运维学习日记--41.Ansible基础入门
linux·运维·学习·ci/cd·自动化·ansible·devops
CIb0la2 小时前
kali linux 2025.2安装WPS并设置无报错的详细步骤
linux·运维·wps
weixin_456904272 小时前
Java泛型与委托
java·spring boot·spring
悟能不能悟2 小时前
能刷java题的网站
java·开发语言
NEUMaple2 小时前
python爬虫(三)----Selenium
爬虫·python·selenium