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

Java 串行接口调用优化

准备面试总结下

 1.CompletableFuture 

 static ThreadPoolExecutor poolExecutor = new ThreadPoolExecutor(10, 20, 1000L, TimeUnit.MICROSECONDS, new ArrayBlockingQueue<>(100));public static void main(String[] args) throws ExecutionException, InterruptedException {CompletableFuture<Integer> task1 = CompletableFuture.supplyAsync(() -> {try {System.out.println("task1");Thread.sleep(1000);} catch (InterruptedException e) {throw new RuntimeException(e);}return 1000;}, poolExecutor);CompletableFuture<Integer> task2 = CompletableFuture.supplyAsync(() -> {try {System.out.println("task2");Thread.sleep(2000);} catch (InterruptedException e) {throw new RuntimeException(e);}return 2000;}, poolExecutor);CompletableFuture<Integer> task3 = CompletableFuture.supplyAsync(() -> {try {System.out.println("task3");Thread.sleep(10000);} catch (InterruptedException e) {throw new RuntimeException(e);}return 5000;}, poolExecutor);Integer result1 = task1.get();System.out.println(result1);Integer result2 = task2.get();System.out.println(result2);Integer result3 = task3.get();System.out.println(result3);CompletableFuture<Void> voidCompletableFuture = CompletableFuture.allOf(task1, task2, task3);poolExecutor.shutdown();System.out.println("执行完毕");}

2.CoutDownLatch 

static HashMap<String, Integer> map = new HashMap<String, Integer>();public static void main(String[] args) throws InterruptedException, ExecutionException {ThreadPoolExecutor poolExecutor = new ThreadPoolExecutor(10, 20, 1000L, TimeUnit.MICROSECONDS, new ArrayBlockingQueue<>(100));CountDownLatch countDownLatch = new CountDownLatch(3);Future<Integer> task1 = poolExecutor.submit(new Callable<Integer>() {@Overridepublic Integer call() throws Exception {System.out.println("任务1");Thread.sleep(1000);System.out.println("任务1结束");countDownLatch.countDown();return 1000;}});Future<Integer> task2 = poolExecutor.submit(new Callable<Integer>() {@Overridepublic Integer call() throws Exception {System.out.println("任务2");Thread.sleep(500);System.out.println("任务2结束");countDownLatch.countDown();return 500;}});Future<Integer> task3 = poolExecutor.submit(new Callable<Integer>() {@Overridepublic Integer call() throws Exception {System.out.println("任务3");Thread.sleep(2000);System.out.println("任务3结束");countDownLatch.countDown();return 2000;}});map.put("task1", task1.get());map.put("task2", task1.get());map.put("task3", task1.get());System.out.println("线程跑完了");countDownLatch.await();System.out.println("---------------任务执行结束-------------");poolExecutor.shutdown();}

3.阻塞获取异步调用结果

   public static void main(String[] args) throws ExecutionException, InterruptedException {ExecutorService executorService = Executors.newFixedThreadPool(3);Future<String> submit = executorService.submit(new Callable<String>() {@Overridepublic String call() throws Exception {Thread.sleep(1000);return "Its done";}});while (true){if (submit.isDone()){System.out.println(submit.get());break;}System.out.println(submit.isDone());}}

4.除了上面两个方法 还有 CyclicBarrier,Semaphore也都可以实现,可以自己尝试下

http://www.lryc.cn/news/190457.html

相关文章:

  • 【Java 进阶篇】JavaScript `typeof` 操作符详解
  • electron之进程间通信
  • Linux网络编程:UDP协议和TCP协议
  • 【SCS-CN】SCS-CN模型中CN值的确定
  • 【C++】继承 ① ( 面向对象特点 | 类之间的关系 | 单继承与多继承 | 继承关系特性 )
  • 虹科方案 | 虹科ATTO加速虚拟存储管理
  • Docker项目部署lnmp+wordpress
  • leetcode 121. 买卖股票的最佳时机、122. 买卖股票的最佳时机 II
  • 系统架构设计:5 论软件的可靠性设计
  • 03 独立看门狗 hal库 stm32cubemx
  • 大数据学习(6)-hive底层原理Mapreduce
  • SQLite:TIMESTAMP类型使用
  • 迅镭激光GI系列高功率激光切割机成功中标覆铜板龙头企业HZ公司
  • 基于SSM的网络安全宣传网站设计与实现
  • k8s修改集群IP--重置集群
  • 记录:R语言生成热图(非相关性)
  • 第55篇-某did滑块流程分析-滑动验证码【2023-10-12】
  • 正点原子嵌入式linux驱动开发——Linux内核顶层Makefile详解
  • C++ 笔记索引
  • Android攻城狮学鸿蒙-配置
  • SpringBoot 接口 字节数组直接显示为图片
  • 黄金票据与白银票据
  • 发稿渠道和发布新闻的步骤和技巧,收藏!
  • 【Leetcode】204. 计数质数
  • LRU自定义最近最少使用-java实现
  • spring:详解spring boot
  • 大数据Doris(八):启动FE步骤
  • vuex常用属性
  • M-LVDS收发器MS2111可pin对pin兼容SN65MLVD206
  • JVM-Java字节码的组成部分