import org.springframework.util.CollectionUtils;
List<List<User>> lists = new ArrayList<>();
//查询业务数据
List<User> userList = dao.queryData();
try {
lists = averageAssign(userList, 10000);
if(!CollectionUtils.isEmpty(lists)) {
lists.foreach(o -> {
new Thread(() -> {
try {
taskCommon(o);
} catch (Exception e) {
}
}).start();
});
}
} catch(Exception e) {
TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
}finally {
lists.clear();
}
public synchronized void taskCommon(List<User> list) {
//业务处理
}
import org.springframework.util.CollectionUtils;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;
public static<T> List<List<T>> averageAssign(List<T> source, int size) {
if(!CollectionUtils.isEmpty(source) && size > 0) {
int num = source.size() % size == 0 ? source.size() / size : (source.size() / size)+1;
return Stream.iterate(0, n -> n + 1).limit(num).parallel().map(a -> source.parallelStream().skip(a * size).limit(size).collect(Collectors.toList())).filter(b -> !b.isEmpty()).collect(Collectors.toList());
}
return null;
}