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();