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));
    }
相关推荐
久绊A22 分钟前
Python 基本语法的详细解释
开发语言·windows·python
Nerd Nirvana2 小时前
软考—系统架构设计(案例 | 论文)
linux·系统架构·软件工程·软考·计算机基础
南山十一少2 小时前
Spring Security+JWT+Redis实现项目级前后端分离认证授权
java·spring·bootstrap
勤奋的凯尔森同学3 小时前
webmin配置终端显示样式,模仿UbuntuDesktop终端
linux·运维·服务器·ubuntu·webmin
Hylan_J4 小时前
【VSCode】MicroPython环境配置
ide·vscode·python·编辑器
莫忘初心丶4 小时前
在 Ubuntu 22 上使用 Gunicorn 启动 Flask 应用程序
python·ubuntu·flask·gunicorn
427724004 小时前
IDEA使用git不提示账号密码登录,而是输入token问题解决
java·git·intellij-idea
chengooooooo4 小时前
苍穹外卖day8 地址上传 用户下单 订单支付
java·服务器·数据库
李长渊哦4 小时前
常用的 JVM 参数:配置与优化指南
java·jvm
计算机小白一个4 小时前
蓝桥杯 Java B 组之设计 LRU 缓存
java·算法·蓝桥杯