当前位置: 首页 > wzjs >正文

网站维护与建设实训心得公司网站没备案

网站维护与建设实训心得,公司网站没备案,网页游戏排行榜2013前十名,今天杭州新闻最新消息一、CompletableFuture 概述 CompletableFuture 是 Java 8 引入的核心并发工具类,实现了 Future 和 CompletionStage 双接口,在异步编程领域具有里程碑意义。与传统的 FutureTask 不同,它不直接实现 Runnable 接口,而是通过高阶函…

    一、CompletableFuture 概述

    CompletableFuture 是 Java 8 引入的核心并发工具类,实现了 Future 和 CompletionStage 双接口,在异步编程领域具有里程碑意义。与传统的 FutureTask 不同,它不直接实现 Runnable 接口,而是通过高阶函数和流水线操作提供了更强大的异步编程能力。其核心特性包括:

    • 非阻塞编程模型:通过回调机制避免线程阻塞
    • 链式组合能力:支持多个异步任务的流水线处理
    • 异常处理机制:提供完整的异常传播和处理方案
    • 执行控制灵活性:支持自定义线程池和并行策略

    与 FutureTask 的关键差异在于:

    • FutureTask 需要显式提交到线程池执行
    • CompletableFuture 自身即代表异步任务,可直接启动
    • 提供超过 50 种组合操作方法,远超 Future 的基础功能

    二、异步任务执行

    2.1 无返回值任务

    // 使用默认线程池(ForkJoinPool)
    CompletableFuture<Void> future = CompletableFuture.runAsync(() -> {System.out.println("Async task without result");
    });// 使用自定义线程池
    ExecutorService customPool = Executors.newFixedThreadPool(4);
    CompletableFuture.runAsync(() -> {// 耗时操作
    }, customPool);

    2.2 带返回值任务

    CompletableFuture<String> future = CompletableFuture.supplyAsync(() -> {// 模拟数据处理return processData(); 
    });// 获取结果(阻塞方式)
    try {String result = future.get(2, TimeUnit.SECONDS);
    } catch (InterruptedException | ExecutionException e) {handleException(e);
    }// 非阻塞获取(推荐方式)
    future.whenComplete((res, ex) -> {if(ex != null) {System.err.println("Error occurred: " + ex.getMessage());} else {System.out.println("Result: " + res);}
    });

    2.3 异常处理对比

    方法

    异常类型

    检查要求

    中断处理

    get()

    ExecutionException

    强制处理

    响应中断

    join()

    CompletionException

    可选处理

    不响应中断

    getNow()

    立即返回预设值

    三、任务组合策略

    3.1 竞速模式(anyOf)

    CompletableFuture<String>[] futures = new CompletableFuture[]{queryFromSourceA(),queryFromSourceB(),queryFromSourceC()
    };CompletableFuture<Object> fastest = CompletableFuture.anyOf(futures);
    fastest.thenAccept(result -> {System.out.println("First response: " + result);
    });

    特性说明:

    • 返回第一个完成的任务结果
    • 异常任务会被忽略(除非全部失败)
    • 结果类型需要手动转型

    3.2 全集模式(allOf)

    CompletableFuture<Void> all = CompletableFuture.allOf(futures);
    all.thenApply(v -> Arrays.stream(futures).map(CompletableFuture::join).collect(Collectors.toList())
    ).thenAccept(results -> {System.out.println("All tasks completed: " + results);
    });

    最佳实践:

    1. 配合 thenApply 收集结果
    2. 使用 join() 保证异常传播
    3. 并行处理独立子任务

    四、异步回调机制

    4.1 结果消费(whenComplete)

    future.whenComplete((result, ex) -> {if(ex != null) {log.error("Processing failed", ex);return;}saveToDatabase(result);
    });

    特点:

    • 无返回值
    • 可访问原始结果和异常
    • 不影响结果传播链

    4.2 结果转换(handle)

    CompletableFuture<Integer> transformed = future.handle((res, ex) -> {return ex != null ? 0 : res.length();
    });

    优势:

    • 可修改返回值类型
    • 异常恢复能力
    • 支持链式转换操作

    五、链式处理模式

    5.1 操作类型对比

    方法

    输入

    输出

    典型应用场景

    thenRun

    日志记录、通知

    thenAccept

    结果存储、状态更新

    thenApply

    数据转换、业务处理

    5.2 执行策略选择

    // 默认线程池(可能切换线程)
    future.thenApplyAsync(...) // 指定线程池(控制资源)
    future.thenApplyAsync(func, customPool)// 同步执行(延续当前线程)
    future.thenApply(...)

    线程策略建议:

    • CPU密集型任务使用通用池
    • IO密集型任务使用定制线程池
    • 快速操作使用同步模式减少开销

    六、最佳实践建议

    1.异常处理统一化

    future.exceptionally(ex -> {return handleFallback(ex);
    });

    2.超时控制机制

    future.orTimeout(3, TimeUnit.SECONDS).exceptionally(ex -> "Timeout handling");

    3.资源管理规范

    try (ExecutorService pool = Executors.newVirtualThreadPerTaskExecutor()) {CompletableFuture.supplyAsync(..., pool);
    }

    4.性能监控集成

    future.whenComplete((res, ex) -> {metrics.recordDuration(System.nanoTime() - startTime);
    });

    通过合理运用 CompletableFuture 的组合特性,开发者可以构建出高效、健壮的异步处理系统。建议结合具体业务场景选择合适的组合策略,并注意线程资源和异常处理的管理,以实现最优的系统性能。


    文章转载自:

    http://0cGt4GFk.qwpyf.cn
    http://RaKHCkml.qwpyf.cn
    http://ZBvMwC9I.qwpyf.cn
    http://v8gJ9OFQ.qwpyf.cn
    http://I92KqDlO.qwpyf.cn
    http://2B8C1zDL.qwpyf.cn
    http://eI70VJ8r.qwpyf.cn
    http://u8yZYhIT.qwpyf.cn
    http://TXdSj4Zt.qwpyf.cn
    http://qDgTdSx6.qwpyf.cn
    http://VjpRnfuz.qwpyf.cn
    http://MaenymqA.qwpyf.cn
    http://QmNhaNKL.qwpyf.cn
    http://bDbeSWNt.qwpyf.cn
    http://QF1PUoa8.qwpyf.cn
    http://d1d7VU73.qwpyf.cn
    http://18H0xxGw.qwpyf.cn
    http://lwnVV2b5.qwpyf.cn
    http://V0Q2KMHp.qwpyf.cn
    http://lDKuamI8.qwpyf.cn
    http://BDnKz9aY.qwpyf.cn
    http://GiO6erus.qwpyf.cn
    http://4X0DUrcm.qwpyf.cn
    http://XisYBzcU.qwpyf.cn
    http://PGvKswzZ.qwpyf.cn
    http://0Xk5jUcE.qwpyf.cn
    http://1sN5rRPl.qwpyf.cn
    http://NMsQ39x5.qwpyf.cn
    http://oK13Evij.qwpyf.cn
    http://yuMDMrP2.qwpyf.cn
    http://www.dtcms.com/wzjs/774219.html

    相关文章:

  • 深圳做营销网站公司哪家好西安网站优化指导
  • 百度数据网站沈阳最新通知今天重要消息
  • icon图标素材下载网站网站建设pdf
  • 网站改版方案案例站长工具关键词挖掘
  • 哪个网站做视频挣钱影视会员网站怎么建设
  • wx5 做网站可以么刚建设的网站多久能在百度查到
  • 做网站所需要的资质wordpress页面自定义
  • php做网站流程网站的外链建设
  • wordpress网站佣金推广双公示网站专栏建设情况
  • 网页在线生成做网站优化有什么途径
  • vscode的网站开发配置装饰设计资质等级标准
  • 深圳网站开发深圳网站设计建站宝盒后台
  • 温州网站建设访问wordpress的xmlrpc.php
  • 商务网站开发实验报告下载建网站
  • 购物网站建设行业现状广东网站建设方便
  • 门户类网站建设电子邮箱免费注册
  • 泰州网站建设要多少钱目前网站开发趋势
  • 广东微信网站制作公司做网站 信息集成过程的顺序
  • 创建网站的六个步骤 天堂最新版在线资源
  • 给别人做网站的话术网站怎么做的qq邮件订阅
  • 网站展示济南网站制作搜到
  • 广州市律师网站建设怎么样百度手机助手应用商店
  • 高密 网站建设西安网站开发技术
  • 网站建设流程图viso网站开发移动端多少钱
  • 网站被抄袭怎么投诉肥乡邯郸做网站
  • 实用软件推荐wordpress头部优化
  • php网站支付宝接口响应式网页源码
  • 建设网站涉及的技术佛山网页设计公司
  • wordpress企业网站seowordpress 4.7 新功能
  • 网站建设平台开发网站建设公司 深圳