多线程记录
java8 lambda CompletableFuture
工作中代码块
private static final Executor EXECUTOR = Executors.newFixedThreadPool(10);
CompletableFuture<List<Order>> orderFuture = CompletableFuture.supplyAsync(() -> {
if (oidAlls.isEmpty()) {
return Collections.emptyList();
}
ShardUtil.setShardContext(shopId);
return orderCommonService.getByShopIdAndOidListWithScale(shopId, oidAlls, 300);
}, EXECUTOR);。
try {
return CompletableFuture.allOf(xxxFuture, xxxFuture, xxxxFuture, rxxxFuture).handleAsync((aVoid, throwable) -> {
try {
List<Trade> tradeAllList = xxxx.get();
return result;
} catch (InterruptedException | ExecutionException e) {
log.error("xxxxx", e);
throw new BusinessException("xxxxx");
}
}).get();
} catch (InterruptedException | ExecutionException e) {
log.error("xxxx", e);
throw new BusinessException("xxxx");
}
简单实例
package com.kyang.test;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.*;
public class CompleTableFutureTest {
private static final ThreadPoolExecutor pool = new ThreadPoolExecutor(2, 2, 30, TimeUnit.SECONDS, new ArrayBlockingQueue<Runnable>(10));
public static void func1(String threaName) {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("threaName :" + threaName + " exectu func1");
}
public static void func2(String threaName) {
try {
Thread.<