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

tp5 网站开发北京最新疫情情况

tp5 网站开发,北京最新疫情情况,怎么做仲博注册网站,网站建设的市场调研分析1. Reactor是什么 Reactor 是一个基于Reactive Streams规范的响应式编程框架。它提供了一组用于构建异步、事件驱动、响应式应用程序的工具和库。Reactor 的核心是 Flux(表示一个包含零到多个元素的异步序列)和 Mono表示一个包含零或一个元素的异步序列…

1. Reactor是什么

  • Reactor 是一个基于Reactive Streams规范的响应式编程框架。它提供了一组用于构建异步、事件驱动、响应式应用程序的工具和库。Reactor 的核心是 Flux(表示一个包含零到多个元素的异步序列)和 Mono表示一个包含零或一个元素的异步序列)。
  • Reactor 通过提供响应式的操作符,如mapfilterflatMap等,使得开发者能够方便地进行数据流的转换和处理。

2. Reactor、Callback、CompletableFuture三种形式异步编码对比

  • 编码简洁程度Reactor最优
  • Reactor线程利用率最高(因实现了Reactive Streams规范,拥有背压+事件驱动特性,此处暂不展开)

代码如下:

pom依赖

<dependencyManagement><dependencies><dependency><groupId>io.projectreactor</groupId><artifactId>reactor-bom</artifactId><version>2023.0.0</version><type>pom</type><scope>import</scope></dependency></dependencies>
</dependencyManagement><dependencies><dependency><groupId>io.projectreactor</groupId><artifactId>reactor-core</artifactId></dependency>
</dependencies>

Callback回调地狱

interface FirstCallback {void onCompleteFirst(String result);void onErrorFirst(Exception e);
}interface SecondCallback {void onCompleteSecond(String result);void onErrorSecond(Exception e);
}interface ThirdCallback {void onCompleteThird(String result);void onErrorThird(Exception e);
}class AsyncOperations {static void firstOperation(FirstCallback firstCallback) {new Thread(() -> {try {// 模拟异步操作Thread.sleep(2000);// 操作完成后调用回调函数firstCallback.onCompleteFirst("First operation completed");} catch (Exception e) {// 发生异常时调用错误回调firstCallback.onErrorFirst(e);}}).start();}static void secondOperation(String input, SecondCallback secondCallback) {new Thread(() -> {try {// 模拟异步操作Thread.sleep(2000);// 操作完成后调用回调函数secondCallback.onCompleteSecond("Second operation completed with input: " + input);} catch (Exception e) {// 发生异常时调用错误回调secondCallback.onErrorSecond(e);}}).start();}static void thirdOperation(String input, ThirdCallback thirdCallback) {new Thread(() -> {try {// 模拟异步操作Thread.sleep(2000);// 操作完成后调用回调函数thirdCallback.onCompleteThird("Third operation completed with input: " + input);} catch (Exception e) {// 发生异常时调用错误回调thirdCallback.onErrorThird(e);}}).start();}
}public class CallbackHellExample {public static void main(String[] args) {AsyncOperations.firstOperation(new FirstCallback() {@Overridepublic void onCompleteFirst(String result) {System.out.println("First Callback: " + result);// 第一次操作完成后调用第二次操作AsyncOperations.secondOperation(result, new SecondCallback() {@Overridepublic void onCompleteSecond(String result) {System.out.println("Second Callback: " + result);// 第二次操作完成后调用第三次操作AsyncOperations.thirdOperation(result, new ThirdCallback() {@Overridepublic void onCompleteThird(String result) {System.out.println("Third Callback: " + result);}@Overridepublic void onErrorThird(Exception e) {System.out.println("Error in Third Callback: " + e.getMessage());}});}@Overridepublic void onErrorSecond(Exception e) {System.out.println("Error in Second Callback: " + e.getMessage());}});}@Overridepublic void onErrorFirst(Exception e) {System.out.println("Error in First Callback: " + e.getMessage());}});// 主线程继续执行其他操作System.out.println("Main thread continues...");}
}

CompletableFuture优化Callback回调地狱

public class CompletableFutureExample {public static void main(String[] args) {CompletableFuture<String> firstOperation = CompletableFuture.supplyAsync(() -> {try {// 模拟异步操作Thread.sleep(2000);return "First operation completed";} catch (InterruptedException e) {throw new RuntimeException(e);}});CompletableFuture<String> secondOperation = firstOperation.thenApplyAsync(result -> {System.out.println("First CompletableFuture: " + result);try {// 模拟异步操作Thread.sleep(2000);return "Second operation completed with input: " + result;} catch (InterruptedException e) {throw new RuntimeException(e);}});CompletableFuture<String> thirdOperation = secondOperation.thenApplyAsync(result -> {System.out.println("Second CompletableFuture: " + result);try {// 模拟异步操作Thread.sleep(2000);return "Third operation completed with input: " + result;} catch (InterruptedException e) {throw new RuntimeException(e);}});thirdOperation.whenComplete((result, throwable) -> {if (throwable == null) {System.out.println("Third CompletableFuture: " + result);} else {System.out.println("Error in CompletableFuture: " + throwable.getMessage());}});// 主线程继续执行其他操作System.out.println("Main thread continues...");// 等待所有操作完成CompletableFuture.allOf(firstOperation, secondOperation, thirdOperation).join();}
}

Reactor优化Callback回调地狱

public class ReactorOptimizedExample {public static void main(String[] args) {Mono.fromCallable(() -> {// 模拟异步操作Thread.sleep(2000);return "First operation completed";}).subscribeOn(Schedulers.boundedElastic()).flatMap(result -> {System.out.println("First Reactor: " + result);return Mono.fromCallable(() -> {// 模拟异步操作Thread.sleep(2000);return "Second operation completed with input: " + result;}).subscribeOn(Schedulers.boundedElastic());}).flatMap(result -> {System.out.println("Second Reactor: " + result);return Mono.fromCallable(() -> {// 模拟异步操作Thread.sleep(2000);return "Third operation completed with input: " + result;}).subscribeOn(Schedulers.boundedElastic());}).doOnSuccess(result -> System.out.println("Third Reactor: " + result)).doOnError(error -> System.out.println("Error in Reactor: " + error.getMessage())).block(); // 阻塞等待操作完成// 主线程继续执行其他操作System.out.println("Main thread continues...");}
}

学习打卡:Java学习笔记-day06-响应式编程Reactor优化Callback回调地狱

http://www.mmbaike.com/news/26977.html

相关文章:

  • wordpress app for ios 3.4下载画质优化app下载
  • b2b电子商务网站的类型不包括seo服务外包客服
  • 做网站选服务器带宽注册百度账号免费
  • 宁波网站推广专业的建站优化公司软件开发app制作公司
  • 网站初期建设方案推广哪个平台好
  • 香港网站后缀推广费用一般多少
  • 寮步营销型网站建设新站优化案例
  • 徐州市城乡和城乡建设厅网站首页域名状态查询工具
  • 推广赚钱的软件排行宁波免费seo在线优化
  • 什么软件是做网站的百度推广售后服务电话
  • 手机移动端网站怎么做的seo自动点击排名
  • 重庆网站建设报价今日重大新闻
  • 网站内链案例网站建设公司seo关键词
  • 广州网站设计开发招聘潍坊seo培训
  • b2c模式类型有哪些seo是什么意思
  • 东升手机网站建设平台运营推广
  • 上饶做网站公司人民政府网站
  • 聊城市建设局网站最新军事新闻今日最新消息
  • 中国物流网站互联网广告投放代理公司
  • p2p网上贷款网站建设方案磁力蜘蛛搜索引擎
  • 南昌做网站的公司哪里好近三天发生的大事
  • html5网站引导页缅甸新闻最新消息
  • 东莞vi设计discuz论坛seo设置
  • 专业图库网站 西安全网推广的方式有哪些
  • 兴扬汽车网站谁做的无锡seo优化公司
  • 公司网站做好了怎么做排名惠州seo全网营销
  • 服装设计网站模板免费制作详情页的网站
  • 网站建设手机端搜索引擎营销的名词解释
  • 用vs2012做简单网站企业内训机构
  • 网站被很多公司抄袭seo关键词分析表