public interface ListCollectionCallback<E extends Object> {
public void call(List<E> collection);
}
实现Runnable,处理业务逻辑
public class ProcessDataFutureCallable implements Runnable {
@Override
public void run() {
//处理业务逻辑
}
}
多线程异步处理业务,通过future同步返回
final List<Future<?>> dataList = new ArrayList<Future<?>>();
final ExecutorService exePool = Executors.newFixedThreadPool(threadNum);
ListCollectionCallback<String> callable = new ListCollectionCallback<String>() {
@Override
public void call(List<String> collection) {
ProcessDataFutureCallable future=new ProcessDataFutureCallable();
dataList.add(exePool.submit(future));
}
};
//剥离业务调用callable
CollectionUtils.splitCollectionHandle(callable);
for (Future<?> future : dataList) {
try {
future.get();
} catch (InterruptedException e) {
logger.warn("线程异常中断", e);
} catch (ExecutionException e) {
logger.warn("线程异常中断", e);
} catch (Exception e) {
logger.warn("发生异常", e);
}
}
exePool.shutdown();