多任务派发线程处理示例supplyAsync
package com.cytc.test;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;
public class T7 {
public static void main(String[] args) {
List<List<String>> groupedIdList = new ArrayList<>();
List<String> temp1 = new ArrayList<>();
temp1.add("123");
List<String> temp2 = new ArrayList<>();
temp2.add("456");
groupedIdList.add(temp1);
groupedIdList.add(temp2);
List<CompletableFuture<List<String>>> futureList = groupedIdList.stream()
.map(taskData -> CompletableFuture.supplyAsync(() -> queryTyGroupList(taskData),
new ThreadPoolExecutor(100, 500, 10L, TimeUnit.SECONDS, new LinkedBlockingQueue(500))))
.collect(Collectors.toList());
// 确保所有的任务完成
List<List<String>> resultBeanList = futureList.stream().map(CompletableFuture::join).collect(Collectors.toList());
System.out.println("******************* all tasks handle done**********************");
resultBeanList.forEach(c->{
System.out.println(c);
});
}
private static List<String> queryTyGroupList(List<String> codeList) {
codeList.forEach(code -> {
code = code + " handle data";
System.out.println(code);
try {
Thread.sleep(new Random().nextInt(1000));
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
});
codeList.add(" new data.. ");
return codeList;
}
}
因为相信,所以看见.

浙公网安备 33010602011771号