在我的 Spring/java 项目中,我想广播有限 channel 调用。假设这里有 500 个联系号码和 10 个网关 channel 。在这里,我想调用 10 x 10 个联系人,并存储在辅助集合
(列表/队列)中,如果给定时间之间有任何调用插入,则 11 个联系人到来,辅助集合再次变为 10。再次重复如此反复,直到所有调用完成。
例如:
List<T> a=(500 contacts);
List<T> b=(10 contacts calls at a time);
if any of calls cut in shortly 11th contact comes in List<T> b in it`s size become 10 again.
任何人都可以提出任何想法来实现这一逻辑吗?
最佳答案
最好将Queue数据结构维护为辅助集合。希望以下代码对您有所帮助。
ArrayList<Contact>a = new ArrayList<>(500);
Queue<Contact>b = new LinkedList<>();
// maintain queue size of size 10
int aIndex = 0;
for (int i = 0; i < 10; ++i) {
b.add(a.get(aIndex++));
}
while (true) {
if (b.isEmpty()) break; // break when all call are completed
Contact frontValue = b.poll(); // get queue's first element and remove it
// assign above contact to channel
// put another contact from a to b
if (aIndex < a.size()) {
b.add(a.get(aIndex++));
}
}
关于Java List 创建大小有限的辅助列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53369083/