This commit is contained in:
xiangwb
2018-02-10 15:57:01 +08:00
parent b548f47e25
commit 3506f16fa3
2 changed files with 14 additions and 12 deletions
@@ -87,20 +87,21 @@ public class LambdaDemo {
}
/**
* 遍历集合,集合里数据还要进行复杂操作,导致速度很慢,可以用异步(无法保证顺序)
* 遍历集合,集合里数据还要进行复杂操作,导致速度很慢,可以用异步
*/
public List<JSONObject> supplyAsync() {
List<JSONObject> list = getList();
public List<Integer> supplyAsync() {
List<Integer> list = new ArrayList<>();
for (int i = 0; i < 100; i++) {
list.add(i);
}
int size = list.size();
CompletableFuture[] futures = new CompletableFuture[size];
List<JSONObject> finalList = new ArrayList<>();
List<Integer> finalList = new ArrayList<>();
for (int i = 0; i < size; i++) {
JSONObject object = list.get(i);
if (object != null) {
futures[i] = CompletableFuture.supplyAsync(() -> finalList.add(setData(object)), taskExecutor);
} else {
futures[i] = CompletableFuture.runAsync(() -> System.out.println("null"));
}
finalList.add(null);//必须有这步,否则会下标越界
Integer integer = list.get(i);
final int pos = i;
futures[i] = CompletableFuture.supplyAsync(() -> finalList.set(pos, integer));//按原来顺序存
}
CompletableFuture.allOf(futures).join();//线程等待,效果等同于get(),会拋出CompletionException
// CompletableFuture<Void> completableFuture = CompletableFuture.allOf(futures);
@@ -17,12 +17,13 @@ public class demo {
for (int i = 0; i < 100; i++) {
list.add(i);
}
List<Integer> finalList = new ArrayList<>();
int size = list.size();
CompletableFuture[] futures = new CompletableFuture[size];
List<Integer> finalList = new ArrayList<>();
for (int i = 0; i < size; i++) {
Integer integer = list.get(i);
futures[i] = CompletableFuture.supplyAsync(() -> finalList.add(integer));
int finalI = i;
futures[i] = CompletableFuture.supplyAsync(() -> finalList.set(finalI, integer));
}
CompletableFuture.allOf(futures).join();
System.out.println(finalList);