将一段list集合的数据进行切割,分段进行处理
public static void main(String[] args) {
final LinkedBlockingQueue<Integer> testQueue = new LinkedBlockingQueue<Integer>(
100);
Random random = new Random();
Integer temp = null;
try {
for (int i = 0; i < 100; i++) {
temp = random.nextInt(10000);
testQueue.put(temp);
System.out.print(temp + ",");
}
} catch (InterruptedException e) {
e.printStackTrace();
}
new Thread(new Runnable() {
@Override
public void run() {
System.out.println("------------------------------");
boolean hasNext = true;
do {
System.out.println("------------------------------");
List<Integer> cutOrderIdColl = doCutOrderIdToList(
testQueue, 30);
hasNext = cutOrderIdColl.size() == 30;
for (int i = 0, size = cutOrderIdColl.size(); i < size; i++) {
System.out.print(cutOrderIdColl.get(i).toString()
.concat(","));
}
System.out.println("------------------------------");
} while (hasNext);
}
}).start();
}
private static List<Integer> doCutOrderIdToList(final LinkedBlockingQueue<Integer> orderIds, int pageSize) {
List<Integer> result = new ArrayList<Integer>(pageSize);
Integer tmp = null;
for (int i = 0; i < pageSize; i++) {
tmp = orderIds.poll();
if (tmp == null) {
break;
}
result.add(tmp);
}
return result;
}