码农知识堂 - 1000bd
  •   Python
  •   PHP
  •   JS/TS
  •   JAVA
  •   C/C++
  •   C#
  •   GO
  •   Kotlin
  •   Swift
  • 多线程——(Callable)(Future)(CompletableFuture)


    Java使用消息队列还是直接使用线程池ExecutorService异步处理?
    AsyncTool框架简介和分析实现

    开启线程的方式

    JAVA开启线程的三种方式
    除了以上三种还可以通过线程池开启

    Callable与Future(带返回值的异步线程)

    Callable接口详解

    CompletableFuture(带返回值升级版)

    当几个异步任务相互关联时就要使用到异步任务编排

    学习 CompletableFuture 进阶之前先掌握两种线程池
    异步编程利器:CompletableFuture详解 |Java 开发实战
    CompletableFuture异步和线程池讲解
    springboot线程池和CompletableFuture配合使用

    当几个异步任务相互关联时就要使用到异步任务编排
    详情可参考b站视频
    CompletableFuture入门

    supplyAsync(开启异步任务,带返回值)

    在这里插入图片描述

    package com.example.shiyan.thread;
    
    import java.util.concurrent.CompletableFuture;
    
    public class CompletableFutureDemo1 {
        public static void main(String[] args) throws InterruptedException {
            long l = System.currentTimeMillis();
            System.out.println("小白进入餐厅");
            System.out.println("小白点餐");
            CompletableFuture<String> cf1 = CompletableFuture.supplyAsync(() -> {
                try {
                    System.out.println("厨师做饭");
                    Thread.sleep(2000);
    
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                return "饭做好了";
            });
            System.out.println("小白打游戏");
            System.out.println(String.format("%s ,小白开吃", cf1.join()));
            System.out.println(System.currentTimeMillis() - l);
        }
    }
    
    

    在这里插入图片描述

    thenCompose(任务A然后紧接着任务B)

    在这里插入图片描述

    package com.example.shiyan.thread;
    
    import java.util.concurrent.CompletableFuture;
    
    public class CompletableFutureDemo2 {
        public static void main(String[] args) throws InterruptedException {
            long l = System.currentTimeMillis();
            System.out.println("小白进入餐厅");
            System.out.println("小白点餐");
            CompletableFuture<String> cf1 = CompletableFuture.supplyAsync(() -> {
                try {
                    Thread.sleep(2000);
                    System.out.println("厨师炒菜");
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                return "番茄炒蛋";
            }).thenCompose(dish -> CompletableFuture.supplyAsync(() -> {
                        try {
                            Thread.sleep(2000);
                            System.out.println("服务员打饭");
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                     return dish+"+米饭";
            }));
            System.out.println("小白打游戏");
            System.out.println(String.format("%s 好了,小白开吃", cf1.join()));
            System.out.println(System.currentTimeMillis() - l);
        }
    }
    

    在这里插入图片描述

    thenCombine(任务a,任务b都完成后接上任务c)

    在这里插入图片描述

    import java.util.concurrent.CompletableFuture;
    
    public class CompletableFutureDemo3 {
        public static void main(String[] args) throws InterruptedException {
            long l = System.currentTimeMillis();
            System.out.println("小白进入餐厅");
            System.out.println("小白点餐");
            CompletableFuture<String> cf1 = CompletableFuture.supplyAsync(() -> {
                try {
                    Thread.sleep(2000);
                    System.out.println("厨师炒菜");
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                return "番茄炒蛋";
            }).thenCombine(CompletableFuture.supplyAsync(()->{
                try {
                    Thread.sleep(2000);
                    System.out.println("服务员煮饭");
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                return "米饭";
            }),(dish,rice)->{
                try {
                    Thread.sleep(2000);
                    System.out.println("服务员打饭");
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                return String.format("%s+%s 好了",dish,rice);
            });
            System.out.println("小白打游戏");
            System.out.println(String.format("%s ,小白开吃", cf1.join()));
            System.out.println(System.currentTimeMillis() - l);
        }
    }
    
    

    在这里插入图片描述

  • 相关阅读:
    Biotinyl Cystamine|CAS:128915-82-2|生物素半胱胺
    VM虚拟机安装ikuai软路由系统
    腾讯地图Javascript API GL
    unity3d:GameFramework+xLua+Protobuf+lua-protobuf,与服务器交互收发协议
    The file Structure
    IOS 发布遇到“Unable to authenticate with App Store Connect”错误咋解决?
    Vite + TypeScript + Node 开发一个简易脚手架
    使用python实现http协议的方法
    Shell脚本编程(一) —— 变量定义(用户自定义变量、位置变量、预定义变量、环境变量)
    NFT Insider#109:The Sandbox推出了首个足球小将 NFT 作品集,YGG Web3 游戏峰会即将开启!
  • 原文地址:https://blog.csdn.net/m0_67290880/article/details/127093020
  • 最新文章
  • 攻防演习之三天拿下官网站群
    数据安全治理学习——前期安全规划和安全管理体系建设
    企业安全 | 企业内一次钓鱼演练准备过程
    内网渗透测试 | Kerberos协议及其部分攻击手法
    0day的产生 | 不懂代码的"代码审计"
    安装scrcpy-client模块av模块异常,环境问题解决方案
    leetcode hot100【LeetCode 279. 完全平方数】java实现
    OpenWrt下安装Mosquitto
    AnatoMask论文汇总
    【AI日记】24.11.01 LangChain、openai api和github copilot
  • 热门文章
  • 十款代码表白小特效 一个比一个浪漫 赶紧收藏起来吧!!!
    奉劝各位学弟学妹们,该打造你的技术影响力了!
    五年了,我在 CSDN 的两个一百万。
    Java俄罗斯方块,老程序员花了一个周末,连接中学年代!
    面试官都震惊,你这网络基础可以啊!
    你真的会用百度吗?我不信 — 那些不为人知的搜索引擎语法
    心情不好的时候,用 Python 画棵樱花树送给自己吧
    通宵一晚做出来的一款类似CS的第一人称射击游戏Demo!原来做游戏也不是很难,连憨憨学妹都学会了!
    13 万字 C 语言从入门到精通保姆级教程2021 年版
    10行代码集2000张美女图,Python爬虫120例,再上征途
Copyright © 2022 侵权请联系2656653265@qq.com    京ICP备2022015340号-1
正则表达式工具 cron表达式工具 密码生成工具

京公网安备 11010502049817号