@Component
public class AsyncExecutor {
@Bean(name = "taskExecutor")
public Executor taskExecutor() {
ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
executor.setCorePoolSize(8);
executor.setMaxPoolSize(16);
executor.setQueueCapacity(20000);
executor.setKeepAliveSeconds(60);
executor.setThreadNamePrefix("taskExecutor-");
executor.initialize();
return executor;
}
@Bean(name = "taskExecutor1")
public Executor taskExecutor1() {
ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
executor.setCorePoolSize(32);
executor.setMaxPoolSize(32);
executor.setQueueCapacity(20000);
executor.setKeepAliveSeconds(60);
executor.setThreadNamePrefix("taskExecutor-");
executor.initialize();
return executor;
}
}
/**
* 查询用户列表
*/
@GetMapping("/test")
public Integer test() {
List<Integer> ids = new ArrayList<>(2)
for (int i = 0
ids.add(i)
}
long start = System.currentTimeMillis()
//计算 completeFuture
List<CompletableFuture<Integer>> collect = ids.stream().map(id -> userService.getUserById(id)).collect(Collectors.toList())
//join()方法会阻塞当前线程,直到Future完成,然后返回Future的结果或抛出异常
Integer join = collect.stream().map(CompletableFuture::join).reduce(Integer::sum).get()
long end = System.currentTimeMillis()
System.out.println("异步耗时:" + (end - start) + "ms")
return join
}
/**
* 查询用户列表
*/
@GetMapping("/test1")
public Integer test1() {
List<Integer> ids = new ArrayList<>(2)
for (int i = 0
ids.add(i)
}
//计算同步
long start1 = System.currentTimeMillis()
Integer i = ids.stream().map(id -> userService.getUserById1(id)).reduce(Integer::sum).get()
long end1 = System.currentTimeMillis()
System.out.println("同步耗时:" + (end1 - start1) + "ms")
return i
}
/**
* 查询用户列表
*/
@GetMapping("/test2")
public Integer test2() {
List<Integer> ids = new ArrayList<>(2)
for (int i = 0
ids.add(i)
}
//计算同步2
long start2 = System.currentTimeMillis()
Integer i2 = ids.parallelStream().map(id -> userService.getUserById1(id)).reduce(Integer::sum).get()
long end2 = System.currentTimeMillis()
System.out.println("同步2耗时:" + (end2 - start2) + "ms")
return i2
}
public interface UserService {
@Async("taskExecutor")
CompletableFuture<Integer> getUserById(int id);
Integer getUserById1(int id);
}
@Service
public class UserServiceImpl implements UserService {
@Autowired
private User1Service user1Service;
static List<Integer> ids=Arrays.asList(1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,
16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,
31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,
46,47,48,49,50,51,52,53,54,55,56,57,58,59,60,
61,62,63,64,65,66,67,68,69,70,71,72,73,74,75,
76,77,78,79,80,81,82,83,84,85,86,87,88,89,90,
91,92,93,94,95,96,97,98,99,100);
@Override
public CompletableFuture<Integer> getUserById(int id) {
List<CompletableFuture<List<User>>> collect = ids.stream().map(e -> user1Service.getUsers(e)).collect(Collectors.toList());
List<User> list = collect.stream().map(CompletableFuture::join).flatMap(List::stream).collect(Collectors.toList());
return CompletableFuture.completedFuture(list.size());
}
@Override
public Integer getUserById1(int id) {
List<CompletableFuture<List<User>>> collect =ids.stream().map(e -> user1Service.getUsers(e)).collect(Collectors.toList());
List<User> list = collect.stream().map(CompletableFuture::join).flatMap(List::stream).collect(Collectors.toList());
return list.size();
}
public interface User1Service {
@Async("taskExecutor1")
CompletableFuture<List<User>> getUsers(int id);
}
@Service
public class User1ServiceImpl implements User1Service {
@Override
public CompletableFuture<List<User>> getUsers(int id) {
String name = Thread.currentThread().getName()
try {
// 1s=1000ms
TimeUnit.MILLISECONDS.sleep(200)
} catch (InterruptedException e) {
throw new RuntimeException(e)
}
List<User> userList = new ArrayList<>()
for (int i = 0
User user = new User()
user.setId((long) i)
user.setName("张三" + name + i)
user.setAge(20)
userList.add(user)
}
return CompletableFuture.completedFuture(userList)
}
}