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

个人网站模板素材做文案公众号策划兼职网站

个人网站模板素材,做文案公众号策划兼职网站,装修怎么做网站,一个静态网站开发考虑什么CompletableFuture异步编程 CompletableFuture介绍与传统 Future 的对比使用方法1. 使用 supplyAsync(有返回值)使用 runAsync(无返回值)指定自定义线程池 处理异步结果1. thenApply:转换结果2.thenAccept:…

CompletableFuture异步编程

  • CompletableFuture介绍
  • 与传统 Future 的对比
  • 使用方法
    • 1. 使用 supplyAsync(有返回值)
    • 使用 runAsync(无返回值)
    • 指定自定义线程池
  • 处理异步结果
    • 1. thenApply:转换结果
    • 2.thenAccept:消费结果
    • 3.thenRun:完成后执行操作
  • 组合任务
    • 1. thenCompose:串联两个任务
    • 2. thenCombine:合并两个任务结果
    • 3. allOf:等待所有任务完成
    • 4. anyOf:任意一个任务完成
  • 异常处理
    • 1. exceptionally:捕获异常并返回默认值
    • 2. handle:无论成功/失败都处理
    • 3. whenComplete:记录日志但不修改结果
  • 完整示例:链式调用 + 异常处理
  • 关键点总结

CompletableFuture介绍

1.基础概念

CompletableFuture 是 Java 8 引入的一个类,用于表示异步计算的结果。它实现了 Future 接口,但比传统的 Future 更强大,支持:

  • 非阻塞操作:通过回调函数处理结果,无需手动调用 get() 阻塞线程。

  • 链式编程:将多个异步任务串联或并联,形成复杂的执行流水线。

  • 异常处理:提供统一的异常捕获和恢复机制。

2. 核心思想

  • 异步编程:将耗时的操作(如I/O、网络请求)交给其他线程执行,主线程继续处理其他任务。

  • 函数式风格:通过 thenApply、thenAccept 等方法,以声明式的方式组合任务。

3. 关键特点

  • 回调驱动:任务完成后自动触发后续操作。

  • 线程池集成:支持自定义线程池,避免资源竞争。

  • 结果依赖管理:轻松处理多个任务之间的依赖关系(如A任务的结果是B任务的输入)。

与传统 Future 的对比

特性FutureCompletableFuture
结果获取阻塞调用 get()非阻塞回调(thenAccept)
任务组合需要手动轮询链式调用(thenApply、thenCompose)
异常处理需在调用代码中处理内置 exceptionally、handle
线程控制依赖 ExecutorService支持自定义线程池
适用场景简单的异步任务复杂的异步流水线

使用方法

1. 使用 supplyAsync(有返回值)

public class MyThreadTest {public static void main(String[] args) throws ExecutionException, InterruptedException {CompletableFuture<String> future = CompletableFuture.supplyAsync(() -> {// 模拟耗时操作try {Thread.sleep(1000);}catch (InterruptedException e){e.printStackTrace();}return "00";});// 获取结果(阻塞)String result = future.get();System.out.println("result:"+result);}
}

使用 runAsync(无返回值)

public class MyThreadTest {public static void main(String[] args) throws ExecutionException, InterruptedException {CompletableFuture<Void> completableFuture = CompletableFuture.runAsync(() -> {try {Thread.sleep(1000);} catch (InterruptedException e) {e.printStackTrace();}System.out.println("sleep 1m");});//等待任务完成completableFuture.get();}
}

指定自定义线程池

public class MyThreadTest {public static void main(String[] args) throws ExecutionException, InterruptedException {ExecutorService executor = Executors.newFixedThreadPool(2);CompletableFuture<String> future = CompletableFuture.supplyAsync(() -> {return "Custom Thread Pool";}, executor);String s = future.get();System.out.println(s);}
}

处理异步结果

1. thenApply:转换结果

thenApply 方法用于在 CompletableFuture 完成时应用一个函数,并返回计算的结果。它返回一个新的 CompletableFuture,该 CompletableFuture 的类型由函数返回值的类型决定。
语法:

public <U> CompletableFuture<U> thenApply(Function<? super T,? extends U> fn);

示例:

public static void main(String[] args) throws ExecutionException, InterruptedException {CompletableFuture<String> future = CompletableFuture.supplyAsync(() -> "Hello").thenApply(s -> s + " World");String s = future.get();System.out.println(s);}

输出
在这里插入图片描述

2.thenAccept:消费结果

thenAccept 方法用于在 CompletableFuture 完成时执行一个消费者(Consumer)操作,但不返回任何值(即它的返回类型是 void)。这通常用于执行一些副作用,比如打印日志、更新UI等,而不关心计算的结果。
语法:

public CompletableFuture<Void> thenAccept(Consumer<? super T> action);

示例:

public class MyThreadTest {public static void main(String[] args) throws ExecutionException, InterruptedException {CompletableFuture<Void> future = CompletableFuture.supplyAsync(() -> "Hello").thenAccept(s -> System.out.println("Result: " + s));future.get();}
}

输出
在这里插入图片描述

3.thenRun:完成后执行操作

public class MyThreadTest {public static void main(String[] args) throws ExecutionException, InterruptedException {CompletableFuture<Void> future = CompletableFuture.supplyAsync(() -> "Hello").thenRun(() -> System.out.println("Task finished"));future.get();}
}

输出
在这里插入图片描述

组合任务

1. thenCompose:串联两个任务

public class MyThreadTest {public static void main(String[] args) throws ExecutionException, InterruptedException {CompletableFuture<String> future = CompletableFuture.supplyAsync(() ->{String a = "Hello";System.out.println(Thread.currentThread().getName() + "-a:" + a);return a;}).thenCompose(s -> CompletableFuture.supplyAsync(() -> {String r = " World";System.out.println(Thread.currentThread().getName() + "-r:" + r);return s + r;}));String s = future.get();// "Hello World"System.out.println(Thread.currentThread().getName() + "s:"+s);}
}

输出:
在这里插入图片描述

2. thenCombine:合并两个任务结果

public class MyThreadTest {public static void main(String[] args) throws ExecutionException, InterruptedException {CompletableFuture<String> future1 = CompletableFuture.supplyAsync(() -> {String h = "Hello";System.out.println(Thread.currentThread().getName() + " h:" + h);return h;});CompletableFuture<String> future2 = CompletableFuture.supplyAsync(() ->{String w = " World";System.out.println(Thread.currentThread().getName() + " w:" + w);return w;});CompletableFuture<String> combined = future1.thenCombine(future2, (s1, s2) -> s1 + " " + s2);String s = combined.get();System.out.println(Thread.currentThread().getName() + " s:" + s);}
}

输出:
在这里插入图片描述

3. allOf:等待所有任务完成

public class MyThreadTest {public static void main(String[] args) throws ExecutionException, InterruptedException {CompletableFuture<String> task1 = CompletableFuture.supplyAsync(() -> "Task1");CompletableFuture<String> task2 = CompletableFuture.supplyAsync(() -> "Task2");CompletableFuture<Void> all = CompletableFuture.allOf(task1, task2);all.thenRun(() -> System.out.println("All tasks completed"));}
}

输出
在这里插入图片描述

4. anyOf:任意一个任务完成

public class MyThreadTest {public static void main(String[] args) throws ExecutionException, InterruptedException {CompletableFuture<String> task1 = CompletableFuture.supplyAsync(() -> {try {Thread.sleep(2000);}catch (InterruptedException e){e.printStackTrace();}return "Task1";});CompletableFuture<String> task2 = CompletableFuture.supplyAsync(() -> "Task2");CompletableFuture<Object> any = CompletableFuture.anyOf(task1, task2);any.thenAccept(result -> System.out.println("First result: " + result)); // 输出 "Task2"}
}

输出
在这里插入图片描述

异常处理

1. exceptionally:捕获异常并返回默认值

public class MyThreadTest {public static void main(String[] args) throws ExecutionException, InterruptedException {CompletableFuture<String> future = CompletableFuture.supplyAsync(() -> {if (true) throw new RuntimeException("Error!");return "Success";}).exceptionally(ex -> "Fallback Value");String s = future.get();// 返回 "Fallback Value"System.out.println(s);}
}

在这里插入图片描述

2. handle:无论成功/失败都处理

public static void main(String[] args) throws ExecutionException, InterruptedException {CompletableFuture<String> future = CompletableFuture.supplyAsync(() -> {if (new Random().nextBoolean()) throw new RuntimeException("Error!");return "Success";}).handle((result, ex) -> {if (ex != null) return "Fallback";return result;});}

3. whenComplete:记录日志但不修改结果

public class MyThreadTest {public static void main(String[] args) throws ExecutionException, InterruptedException {CompletableFuture<String> future = CompletableFuture.supplyAsync(() -> "Hello").whenComplete((result, ex) -> {if (ex != null) ex.printStackTrace();else System.out.println("Result: " + result);});String s = future.get();System.out.println(s);}
}

完整示例:链式调用 + 异常处理

public class MyThreadTest {public static void main(String[] args) throws ExecutionException, InterruptedException {CompletableFuture.supplyAsync(() -> {// 步骤1:获取用户IDreturn 123;}).thenApply(userId -> {// 步骤2:根据用户ID查询名称if (userId == 123) return "Alice";else throw new IllegalArgumentException("Invalid User ID");}).thenApply(userName -> {// 步骤3:转换为大写return userName.toUpperCase();}).exceptionally(ex -> {// 统一异常处理System.out.println("Error: " + ex.getMessage());return "DEFAULT_USER";}).thenAccept(finalResult -> {System.out.println("Final Result: " + finalResult); // 输出 "ALICE" 或 "DEFAULT_USER"});}
}

输出
在这里插入图片描述

关键点总结

异步执行:使用 supplyAsync/runAsync 启动异步任务。

链式调用:通过 thenApply/thenAccept/thenRun 串联操作。

组合任务:thenCompose(依赖)和 thenCombine(并行)合并结果。

异常处理:优先使用 exceptionally 或 handle 提供容错。

线程池控制:避免使用默认线程池处理阻塞任务(如I/O)

http://www.dtcms.com/wzjs/834291.html

相关文章:

  • 电子商务网站建设需要哪种语言阿里云服务器可以做网站吗
  • 宁波营销网站建设今天的国际新闻最新消息
  • 商丘企业做网站做招聘网站没有数据
  • 台州做网站wordpress 更改模板路径
  • 登录建设银行网站打不开网站建设是学哪个学科
  • 宝安led行业网站建设搜索关键词的工具
  • 装修行业 网站建设设计类专业网站有哪些
  • 简述制作网站的流程网站设计公司天津
  • 那个网站教做仙芋鲜做外贸一般总浏览的网站
  • 网站icp备案证书下载工作室主题网站
  • 黄山公司做网站做淘宝客网站用什么程序好
  • 网站html有趣代码网站宝的作用
  • 城乡住房建设部网站网络架构模式有什么
  • 软件源码购买一般在哪个网站濮阳市城乡一体化示范区
  • 网站无法上传照片怎么搭建自己的网站后台
  • 看室内设计案例的网站江西网站开发哪家好
  • 表单大师 做网站网站制作+网站建设
  • 广告手机网站制作江苏大汉建设实业集团网站
  • 收到网站建设费分录网站建设中可能遇到的问题
  • 做网站宣传图的网站怎么修改wordpress 字体
  • 长春火车站位置网站关键词选取的步骤
  • 深圳外贸网站推广发新闻稿平台
  • 电商网站用php做的吗seo研究
  • 网站做成微信小程序wordpress显示当前文章的子分类
  • 网站后台插件自己做网站用什么数据库
  • 四川高速公路建设开发集团有限公司网站青岛seo推广公司
  • 德州做网站dzqifanwordpress注册邮件怎么设置
  • 网站建设列入管理费用说明科目微信小程序网站模板
  • 找兼职工作在家做正规网站织梦网站怎么做投票
  • 企业做网站的必要性做电影网站用什么虚拟主机