list平均分配给多个线程算法
int num = 20;
ExecutorService service = Executors.newFixedThreadPool(num);
int rows = 3568;
int pageSize = 100;
int pageSum = (rows - 1) / pageSize + 1;
List<Integer> list = new ArrayList<>();
for (int i = 1; i <= pageSum; i++){
list.add(i);
}
int baseNum = pageSum / num;
int remainderNum = pageSum % num;
int end = 0;
for (int i = 0; i <num; i++)
{
int start = end ;
end = start + baseNum;
if(i == (num-1)){
end = pageSum;
}else if( i < remainderNum){
end = end + 1;
}
int finalEnd = end;
service.submit(
new Runnable() {
@Override
public void run() {
List<Integer> listSmall = list.subList(start, finalEnd );
try {
for (Integer integer : listSmall)
{
System.out.println(integer);
}
} catch (Exception e) {
e.printStackTrace();
}
}
});
}