Java 串行接口调用优化

2023-10-13 06:30

本文主要是介绍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也都可以实现,可以自己尝试下

这篇关于Java 串行接口调用优化的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/201374

相关文章

SpringBoot简单整合ElasticSearch实践

《SpringBoot简单整合ElasticSearch实践》Elasticsearch支持结构化和非结构化数据检索,通过索引创建和倒排索引文档,提高搜索效率,它基于Lucene封装,分为索引库、类型... 目录一:ElasticSearch支持对结构化和非结构化的数据进行检索二:ES的核心概念Index:

Java方法重载与重写之同名方法的双面魔法(最新整理)

《Java方法重载与重写之同名方法的双面魔法(最新整理)》文章介绍了Java中的方法重载Overloading和方法重写Overriding的区别联系,方法重载是指在同一个类中,允许存在多个方法名相同... 目录Java方法重载与重写:同名方法的双面魔法方法重载(Overloading):同门师兄弟的不同绝

Spring配置扩展之JavaConfig的使用小结

《Spring配置扩展之JavaConfig的使用小结》JavaConfig是Spring框架中基于纯Java代码的配置方式,用于替代传统的XML配置,通过注解(如@Bean)定义Spring容器的组... 目录JavaConfig 的概念什么是JavaConfig?为什么使用 JavaConfig?Jav

Java数组动态扩容的实现示例

《Java数组动态扩容的实现示例》本文主要介绍了Java数组动态扩容的实现示例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧... 目录1 问题2 方法3 结语1 问题实现动态的给数组添加元素效果,实现对数组扩容,原始数组使用静态分配

Java中ArrayList与顺序表示例详解

《Java中ArrayList与顺序表示例详解》顺序表是在计算机内存中以数组的形式保存的线性表,是指用一组地址连续的存储单元依次存储数据元素的线性结构,:本文主要介绍Java中ArrayList与... 目录前言一、Java集合框架核心接口与分类ArrayList二、顺序表数据结构中的顺序表三、常用代码手动

JAVA项目swing转javafx语法规则以及示例代码

《JAVA项目swing转javafx语法规则以及示例代码》:本文主要介绍JAVA项目swing转javafx语法规则以及示例代码的相关资料,文中详细讲解了主类继承、窗口创建、布局管理、控件替换、... 目录最常用的“一行换一行”速查表(直接全局替换)实际转换示例(JFramejs → JavaFX)迁移建

Spring Boot Interceptor的原理、配置、顺序控制及与Filter的关键区别对比分析

《SpringBootInterceptor的原理、配置、顺序控制及与Filter的关键区别对比分析》本文主要介绍了SpringBoot中的拦截器(Interceptor)及其与过滤器(Filt... 目录前言一、核心功能二、拦截器的实现2.1 定义自定义拦截器2.2 注册拦截器三、多拦截器的执行顺序四、过

JAVA线程的周期及调度机制详解

《JAVA线程的周期及调度机制详解》Java线程的生命周期包括NEW、RUNNABLE、BLOCKED、WAITING、TIMED_WAITING和TERMINATED,线程调度依赖操作系统,采用抢占... 目录Java线程的生命周期线程状态转换示例代码JAVA线程调度机制优先级设置示例注意事项JAVA线程

JavaWeb项目创建、部署、连接数据库保姆级教程(tomcat)

《JavaWeb项目创建、部署、连接数据库保姆级教程(tomcat)》:本文主要介绍如何在IntelliJIDEA2020.1中创建和部署一个JavaWeb项目,包括创建项目、配置Tomcat服务... 目录简介:一、创建项目二、tomcat部署1、将tomcat解压在一个自己找得到路径2、在idea中添加

Java使用Spire.Doc for Java实现Word自动化插入图片

《Java使用Spire.DocforJava实现Word自动化插入图片》在日常工作中,Word文档是不可或缺的工具,而图片作为信息传达的重要载体,其在文档中的插入与布局显得尤为关键,下面我们就来... 目录1. Spire.Doc for Java库介绍与安装2. 使用特定的环绕方式插入图片3. 在指定位