Java 8 完全未来内部的完全未来
我的代码中有两个完全的未来。在一个可完成的未来中,我想要另一个可完成的未来Java 8 完全未来内部的完全未来,java-8,completable-future,Java 8,Completable Future,我的代码中有两个完全的未来。在一个可完成的未来中,我想要另一个可完成的未来 public CompletableFuture buildEmployee(列出employeeInfo){ 返回SupplySync(()->{ 地图奖励=奖励(employeeInfo); 地图点=点(employeeInfo); },遗嘱执行人); } 在上面的方法中,奖励和积分是两个独立的顺序调用,我希望它能够并行调用它们,因为我尝试了- public CompletableFuture<List<
public CompletableFuture buildEmployee(列出employeeInfo){
返回SupplySync(()->{
地图奖励=奖励(employeeInfo);
地图点=点(employeeInfo);
},遗嘱执行人);
}
在上面的方法中,奖励和积分是两个独立的顺序调用,我希望它能够并行调用它们,因为我尝试了-
public CompletableFuture<List<Employee>> buildEmployee (List<EmployeeResponse> employeeInfo) {
return supplyAsync(() -> {
CompletableFuture<Map<String, List<EmployeeReward>>> rewards = reward(employeeInfo);
CompletableFuture<Map<String, List<EmployeePoints>>> points = points(employeeInfo);
CompletableFuture<Void> futures = allOf(rewards, points);
}, executor);
}
public CompletableFuture buildEmployee(列出employeeInfo){
返回SupplySync(()->{
CompletableFuture rewards=奖励(employeeInfo);
CompletableFuture points=积分(employeeInfo);
CompletableFuture futures=allOf(奖励、积分);
},遗嘱执行人);
}
,如下所示
employeeInfo.stream.map(employee -> Employee.builder().
.<someEmplyInfo>
.points(points.getOrDefault(employee.getEmpId, newArrayList()))
);
employeeInfo.stream.map(employee->employee.builder()。
.
.points(points.getOrDefault(employee.getEmpId,newArrayList())
);
在上述方法中,您使用Async
线程执行buildEmployee
方法,这意味着Async
线程负责执行两个API调用奖励和积分,然后它将合并结果。因此,在上述方法中,该方法是异步执行的,而不是API调用
但是,您可以通过异步进行API调用的另一种方式来实现,使用supplyAsync
异步执行奖励
调用,然后使用主线程执行点
调用。最后阻塞主线程,直到异步调用完成,然后合并结果
public CompletableFuture<List<Employee>> buildEmployee (List<EmployeeResponse> employeeInfo) {
// First call is Async call
CompletableFuture<Map<String, List<EmployeeReward>>> rewards = CompletableFuture.supplyAsync(()->reward(employeeInfo), executor);
//Second call by main thread
Map<String, List<EmployeePoints>>> points = points(employeeInfo);
// main thread is Blocked and get the result of the future.
rewards.get(); //throws InterruptedException,ExecutionException
// Now combine the result and return list
return CompletableFuture.completedFuture(result);
}
public CompletableFuture buildEmployee(列出employeeInfo){
//第一个调用是异步调用
CompletableFuture奖励=CompletableFuture.supplyAsync(()->奖励(employeeInfo),执行者);
//主线程的第二次调用
地图>点=点(员工信息);
//主线程被阻塞并获得将来的结果。
rewards.get();//抛出InterruptedException、ExecutionException
//现在合并结果和返回列表
返回CompletableFuture.completedFuture(结果);
}
处理个别期货异常区块中的任何异常非常重要
理想情况下,控制流不应该依赖于异常处理逻辑,它应该被包装在一个状态对象中,可以用来评估是否应该进行进一步的处理。
在allOf方法中添加一个thenApply post,然后在thenApply块中获取结果应该可以做到这一点
public CompletableFuture<List<Employee>> buildEmployee(List<EmployeeResponse> employeeInfo) {
//Build the future instance for reward
CompletableFuture<Map<String, List<EmployeeReward>>> rewardsFuture = reward(employeeInfo)
.exceptionally(throwable -> {
//Handle the error
return null;
});
//Build the future instance for points
CompletableFuture<Map<String, List<EmployeePoints>>> pointsFuture = points(employeeInfo)
.exceptionally(throwable -> {
//Handle the error for rewards
return null;
});
return CompletableFuture.allOf(rewardsFuture, pointsFuture).thenApply(v -> {
try {
Map<String, List<EmployeeReward>> rewardsResult = rewardsFuture.get();
Map<String, List<EmployeePoints>> pointsResult = pointsFuture.get();
//Convert the above map to the desired list of string
List<Employee> buildEmployeeResult = null;
return buildEmployeeResult;
}
catch (Exception e) {
//Handle exception
return null;
}
}, executor);
}
private CompletableFuture<Map<String, List<EmployeePoints>>> points(List<EmployeeResponse> employeeInfo) {
return supplyAsync(() -> {
//Logic for points synchronous
});
}
private CompletableFuture<Map<String, List<EmployeeReward>>> reward(List<EmployeeResponse> employeeInfo) {
return supplyAsync(() -> {
//Logic for rewards synchronous
});
}
public CompletableFuture buildEmployee(列出employeeInfo){
//为奖励构建未来实例
可完成的未来报酬未来=报酬(员工信息)
.例外情况下(可丢弃->{
//处理错误
返回null;
});
//构建点的未来实例
CompletableFuture points未来=点数(employeeInfo)
.例外情况下(可丢弃->{
//处理错误以获得奖励
返回null;
});
返回CompletableFuture.allOf(报酬未来,积分未来)。然后应用(v->{
试一试{
Map rewardsResult=rewardsFuture.get();
Map pointsResult=pointsFuture.get();
//将上述映射转换为所需的字符串列表
List buildEmployeeResult=null;
返回buildEmployeeResult;
}
捕获(例外e){
//处理异常
返回null;
}
},遗嘱执行人);
}
私人CompletableFuture积分(列出员工信息){
返回SupplySync(()->{
//同步点逻辑
});
}
私人CompletableFuture奖励(列出员工信息){
返回SupplySync(()->{
//奖励同步逻辑
});
}
定义“不工作”我更新了问题。我的意思是仍然需要很长时间才能返回,所以我的问题是如何改进它,或者这是一种正确的方法。方法奖励(…)
和积分(…)
的签名是什么?你给他们的称呼前后不一致。如何从这两种方法的输出构建列表
?换句话说,请包含此方法的顺序版本,该版本可以正确编译和工作。reward(…)
和points(…)
方法分别调用API以获取详细信息。一旦它获得了详细信息,它就会收集到列表中,并将其放在empId的映射中。但是List buildEmployee
是另一个可完成的未来-CompletableFuture employee=employeeInfo.isPresent()?employeeService.buildEmployee(employeeInfo.get()):completedFuture(null)除非您有信心或有理由不这样做,否则应使用代码>异常块@迪帕克。使用.thenApplyAsync不会有任何区别,因为在方法块buildEmployee(…)中,没有任何其他正在执行或预期与.thenApplyAsync(…)并行的操作。两个.get(…)操作已经是未来操作,将在.ThenApplySync(…)内完成。使用.thenApply(…)代替as.thenApplyAsync(…)将不会获得应有的好处,并可能使开发人员混淆行为。同意@Nimeshmakwana
public CompletableFuture<List<Employee>> buildEmployee(List<EmployeeResponse> employeeInfo) {
//Build the future instance for reward
CompletableFuture<Map<String, List<EmployeeReward>>> rewardsFuture = reward(employeeInfo)
.exceptionally(throwable -> {
//Handle the error
return null;
});
//Build the future instance for points
CompletableFuture<Map<String, List<EmployeePoints>>> pointsFuture = points(employeeInfo)
.exceptionally(throwable -> {
//Handle the error for rewards
return null;
});
return CompletableFuture.allOf(rewardsFuture, pointsFuture).thenApply(v -> {
try {
Map<String, List<EmployeeReward>> rewardsResult = rewardsFuture.get();
Map<String, List<EmployeePoints>> pointsResult = pointsFuture.get();
//Convert the above map to the desired list of string
List<Employee> buildEmployeeResult = null;
return buildEmployeeResult;
}
catch (Exception e) {
//Handle exception
return null;
}
}, executor);
}
private CompletableFuture<Map<String, List<EmployeePoints>>> points(List<EmployeeResponse> employeeInfo) {
return supplyAsync(() -> {
//Logic for points synchronous
});
}
private CompletableFuture<Map<String, List<EmployeeReward>>> reward(List<EmployeeResponse> employeeInfo) {
return supplyAsync(() -> {
//Logic for rewards synchronous
});
}