# my-async
**Repository Path**: madaoEE/my-async
## Basic Information
- **Project Name**: my-async
- **Description**: 一个简单实现的异步框架
- **Primary Language**: Unknown
- **License**: Not specified
- **Default Branch**: master
- **Homepage**: None
- **GVP Project**: No
## Statistics
- **Stars**: 3
- **Forks**: 1
- **Created**: 2024-10-13
- **Last Updated**: 2025-06-01
## Categories & Tags
**Categories**: Uncategorized
**Tags**: None
## README
# my-async
#### 介绍
一个简单实现的异步框架,通过注解对方法、类对象添加异步操作
核心技术:Java + 线程池 + 动态代理/AOP
#### 设计目的
并行执行可以大幅度提升程序的运行速度,有效利用 CPU 资源。
但是单独为每次方法都使用线程池手写,显然不够优雅,复用性也很差。
#### 执行流程
与传统执行对比

具体实现:Java环境采用动态代理实现调用;Spring环境采用AOP实现代理调用

#### 快速入门
##### 需要的环境:
- JDK 17
- Maven 3.x+
##### 快速使用
下面以SpringBoot环境做测试
###### 引入依赖
```
org.pxl
async-springboot-starter
1.0-SNAPSHOT
```
###### 添加测试对象
1、实现接口
```
import org.pxl.core.model.AsyncResult;
/**
* @author MADAO
* @create 2024 - 10 - 13 17:35
*/
public interface UserService {
public AsyncResult queryUser(String id);
public AsyncResult query(String id);
}
```
2、接口实现类
```
import org.pxl.api.annotation.Async;
import org.pxl.core.model.AsyncResult;
import org.springframework.stereotype.Service;
import java.util.concurrent.TimeUnit;
/**
* 使用异步框架
*
* @author MADAO
* @create 2024 - 10 - 13 15:37
*/
@Service
public class UserServiceImpl implements UserService{
//异步方法
@Async
public AsyncResult queryUser(String id) {
System.out.println("开始根据用户id 查询用户信息 " + id);
try {
// 沉睡模拟处理耗时
Thread.sleep(300);
} catch (InterruptedException e) {
e.printStackTrace();
}
final String result = id + "-result";
System.out.println("结束根据用户id 查询用户信息 " + result);
AsyncResult asyncResult = new AsyncResult<>();
asyncResult.setValue(result);
return asyncResult;
}
//非异步方法
@Override
public AsyncResult query(String id) {
System.out.println("开始根据用户id 查询用户信息 " + id);
try {
// 沉睡模拟处理耗时
Thread.sleep(300);
} catch (InterruptedException e) {
e.printStackTrace();
}
final String result = id + "-result";
System.out.println("结束根据用户id 查询用户信息 " + result);
AsyncResult asyncResult = new AsyncResult<>();
asyncResult.setValue(result);
return asyncResult;
}
}
```
###### 开始测试
添加测试类:
```
package com.example.asyncspringboottest;
import com.example.asyncspringboottest.service.UserService;
import org.junit.jupiter.api.Test;
import org.pxl.core.model.AsyncResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
@SpringBootTest
class AsyncSpringbootTestApplicationTests {
@Autowired
private UserService userService;
@Test
public void test() {
long start = System.currentTimeMillis();
// 异步测试
// AsyncResult result = userService.queryUser("123");
// AsyncResult result2 = userService.queryUser("1234");
AsyncResult result = userService.query("123");
AsyncResult result2 = userService.query("1234");
System.out.println("查询结果" + result.getResult());
System.out.println("查询结果" + result2.getResult());
long end = System.currentTimeMillis();
System.out.println("共计耗时: " + (end-start));
}
}
```
非异步测试结果:

异步测试结果:

方法实现效率高了一倍
##### 待优化
1. 返回对象是默认对象,减少类的加入
2. 补充自定义线程池,通过@EnableAsync指定
##### 补充
SpringBoot环境测试代码是test分支