线程池工具类
public class ThreadPoolUtil {
public static final long DEFAULT_WAIT_SECONDS = 5000;
private static class ThreadPool {
private static final ThreadFactory namedThreadFactory =
new ThreadFactoryBuilder().setNameFormat("thread-pool-%d").build();
private static final ThreadPoolExecutor pool = new ThreadPoolExecutor(20, 40, 60L, TimeUnit.SECONDS,
new LinkedBlockingQueue<Runnable>(200), namedThreadFactory, new ThreadPoolExecutor.DiscardPolicy()) {
@Override
protected void afterExecute(Runnable r, Throwable t) {
super.afterExecute(r, t);
printException(r, t);
}
};
}
/**
* 获取线程池.
*/
public static ExecutorService getThreadPool() {
log.info("ThreadPool task num now: {}", ThreadPool.pool.getActiveCount());
return ThreadPool.pool;
}
private static void printException(Runnable r, Throwable t) {
if (t == null && r instanceof Future<?>) {
try {
Future<?> future = (Future<?>)r;
if (future.isDone()) {
future.get();
}
} catch (CancellationException ce) {
t = ce;
} catch (ExecutionException ee) {
t = ee.getCause();
} catch (InterruptedException ie) {
Thread.currentThread().interrupt(); // ignore/reset
}
}
if (t != null) {
log.error(t.getMessage(), t);
}
}
/**
* 使用线程池并发执行任务.
*
* @param callables
* 任务列表
*
* @param waitSeconds
* 等待时间(秒)
*
* @return.
*/
public static <T> List<T> executeTasks(List<Callable<T>> callables, long waitSeconds) {
return doExecute(callables, waitSeconds, ThreadPool.pool);
}
private static <T> List<T> doExecute(List<Callable<T>> callables, long waitSeconds, ThreadPoolExecutor executor) {
try {
List<T> result = Lists.newArrayList();
if (callables == null || callables.isEmpty()) {
return result;
}
List<Future<T>> futures = executor.invokeAll(callables);
try {
for (Future<T> future : futures) {
result.add(future.get(waitSeconds, TimeUnit.SECONDS));
}
} catch (Exception e) {
throw e;
} finally {
for (Future<T> future : futures) {
try {
future.cancel(true);
} catch (Exception e2) {
}
}
}
return result;
} catch (Exception e) {
log.error("doExecute error", e);
throw new RuntimeException(e.getMessage());
}
}
}
使用Callable
public String getTableOwnersByTableGuid(List<String> tableGuids) {
String ret = "[";
List<Callable<String>> callables = Lists.newArrayList();
tableGuids.stream().forEach(tableGuid -> {
Callable<String> callable = () -> Optional.ofNullable(odpsTableManager.queryOdpsTableBaseInfo(tableGuid)).map(
GetMetaTableBasicInfoResponse.Data::getOwnerId).orElse(null);
callables.add(callable);
});
List<String> ownerIdList = ThreadPoolUtil.executeTasks(callables, ThreadPoolUtil.DEFAULT_WAIT_SECONDS).stream().filter(
Objects::nonNull).collect(Collectors.toList());
for (String ownerId : ownerIdList) {
ret += "'" + ownerId + "',";
}
return StringUtils.stripEnd(ret, ",") + "]";
}
使用CompletableFuture
public String getTableOwnersByTableGuid1(List<String> tableGuids) {
String ret = "[";
List<CompletableFuture<String>> completableFutureList = Lists.newArrayList();
tableGuids.stream().forEach(tableGuid -> {
try {
CompletableFuture<String> future = CompletableFuture.supplyAsync(() -> odpsTableManager.queryOdpsTableBaseInfo(tableGuid).getOwnerId());
completableFutureList.add(future);
} catch (Exception e) {
log.info("getTableOwnersByWorkOrderId queryOdpsTableBaseInfo fail");
}
});
List<String> ownerIdList = completableFutureList.stream().map(CompletableFuture::join).collect(Collectors.toList());
for (String ownerId : ownerIdList) {
ret += "'" + ownerId + "',";
}
return StringUtils.stripEnd(ret, ",") + "]";
}