merge
This commit is contained in:
@@ -83,11 +83,11 @@ public class LambdaDemo {
|
||||
reduce = optional.get();
|
||||
}
|
||||
//异步回调
|
||||
List<JSONObject> sysUsers = CompletableFuture.supplyAsync(LambdaDemo::getList).join();
|
||||
List<JSONObject> sysUsers = CompletableFuture.supplyAsync(LambdaDemo::getList).join();//线程等待,效果等同于get(),会拋出CompletionException
|
||||
}
|
||||
|
||||
/**
|
||||
* 遍历集合,集合里数据还要进行复杂操作,导致速度很慢,可以用异步
|
||||
* 遍历集合,集合里数据还要进行复杂操作,导致速度很慢,可以用异步(无法保证顺序)
|
||||
*/
|
||||
public List<JSONObject> supplyAsync() {
|
||||
List<JSONObject> list = getList();
|
||||
|
||||
@@ -1,5 +1,8 @@
|
||||
package com.xwbing.demo;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
import java.util.concurrent.ExecutionException;
|
||||
|
||||
/**
|
||||
@@ -10,6 +13,18 @@ import java.util.concurrent.ExecutionException;
|
||||
*/
|
||||
public class demo {
|
||||
public static void main(String[] args) throws ExecutionException, InterruptedException {
|
||||
|
||||
List<Integer> list = new ArrayList<>();
|
||||
for (int i = 0; i < 100; i++) {
|
||||
list.add(i);
|
||||
}
|
||||
List<Integer> finalList = new ArrayList<>();
|
||||
int size = list.size();
|
||||
CompletableFuture[] futures = new CompletableFuture[size];
|
||||
for (int i = 0; i < size; i++) {
|
||||
Integer integer = list.get(i);
|
||||
futures[i] = CompletableFuture.supplyAsync(() -> finalList.add(integer));
|
||||
}
|
||||
CompletableFuture.allOf(futures).join();
|
||||
System.out.println(finalList);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user