Java HttpUtil 异步不等待调用

步骤一:添加依赖

复制代码
<dependencies>
    <dependency>
        <groupId>cn.hutool</groupId>
        <artifactId>hutool-all</artifactId>
        <version>5.6.3</version>
    </dependency>
</dependencies>

步骤二:创建异步请求

复制代码
public class AsyncHttpExample {
    public static void main(String[] args) {
        HttpRequest.get("
                .timeout(2000)
                .executeAsync()
                .thenAccept(response -> {
                    // 处理响应结果
                    System.out.println(response.body());
                });
    }
}

步骤三:等待异步请求完成

复制代码
public class AsyncHttpExample {
    public static void main(String[] args) {
        HttpRequest.get("
                .timeout(2000)
                .executeAsync()
                .thenAccept(response -> {
                    // 处理响应结果
                    System.out.println(response.body());
                })
                .join(); // 等待异步请求完成
        System.out.println("异步请求已完成");
    }
}

总结

使用executeAsync()方法发送异步请求。使用thenAccept()方法处理异步请求的响应结果,并通过join()方法等待异步请求完成。

相关推荐
李白的粉11 分钟前
基于springboot的新闻资讯系统
java·spring boot·毕业设计·课程设计·源代码·新闻资讯系统
软件开发技术深度爱好者11 分钟前
Python类中方法种类介绍
开发语言·python
麦麦鸡腿堡12 分钟前
Java_LinkedList底层结构
java·开发语言
whatever who cares20 分钟前
android/java中gson的用法
android·java·开发语言
摇滚侠22 分钟前
Spring Boot3零基础教程,为什么有Reactive-Stream 规范,响应式编程,笔记101
java·spring boot·笔记
周杰伦fans32 分钟前
C# 中 Entity Framework (EF) 和 EF Core 里的 `AsNoTracking` 方法
开发语言·c#
小灰灰搞电子36 分钟前
Rust Slint实现控件尺寸的扩展与收缩源码分享
开发语言·后端·rust
☆cwlulu37 分钟前
git分支管理详解
开发语言·git·青少年编程
天天摸鱼的java工程师37 分钟前
八年 Java 开发手敲:SpringBoot+SpringSecurity+JWT 实战,前后分离权限注解落地就能跑
java·后端
冰淇淋@39 分钟前
idea启动项目报错java: OutOfMemoryError: insufficient memory
java·ide·intellij-idea